简体   繁体   English

(if)语句在从另一个php文件中包含时不起作用

[英](If) statement not working when including it from another php file

I have two php files first is sample.php and the second is if.php . 我有两个php文件,第一个是sample.php ,第二个是if.php I have some code written in sample.php basically declaration of two variables and an else statement. 我在sample.php写了一些代码,基本上声明了两个变量和一个else语句。 I have the if statement in if.php file which I'm including before else statement. 我在if.php文件中有if语句,要在else语句之前包括它。 But on running my code its shows an error: 但是在运行我的代码时会显示错误:

Unexpected 'else'...etc. 意外的“其他” ...等。

Can you please help me figuring out what is wrong with the code? 您能帮我弄清楚代码出什么问题吗?

Sample.php Sample.php

<?php
$a=10;
$b=20;

include 'if.php';
else
{
echo $b.' is greater';
}
?>

if.php if.php

<?php
if($a>$b)
{
echo $a.' is greater';
}
?>
include 'if.php';
else

Nice, there is no such thing as include else . 尼斯,有没有这样的事情include else Even if you have an if clause at the end of your included file, the include line breaks its sequence. 即使在包含文件的末尾有一个if子句, includeif中断其顺序。

else can only come after either an if or an else if block, no third option. else只能在ifelse if块之后,没有第三个选项。 Period. 期。

Here is how PHP parser will see your 2 files 这是PHP解析器将如何查看您的2个文件的方法

if($a>$b){
  echo $a.' is greater';
}
include "if.php";
else {
  echo 'na';
}

Which is clearly invalid. 这显然是无效的。

If you still want to keep things as they are, like you mentioned in the comments. 如果您仍然想保持现状,就像您在评论中提到的那样。 You can do 1 small trick (amongst many other methods). 您可以做1个小技巧(在许多其他方法中)。 Make your else another if which is contrary to the first if . 让你的else另一if这是违反第一if Like 喜欢

if.php if.php

if($a>$b){
  echo $a.' is greater';
}

main file 主文件

include "if.php";
if($a<=$b) {
  echo 'na';
}

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. include(或require)语句获取指定文件中存在的所有文本/代码/标记,并将其复制到使用include语句的文件中。

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. 当您要在网站的多个页面上包含相同的PHP,HTML或文本时,包含文件非常有用。

The include and require statements are identical, except upon failure: include和require语句相同,但失败时除外:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script require将产生致命错误(E_COMPILE_ERROR)并停止脚本
  • include will only produce a warning (E_WARNING) and the script will continue. 包含只会产生警告(E_WARNING),脚本将继续。

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. 因此,即使您希望执行继续进行并向用户显示输出,即使缺少包含文件,也可以使用include语句。 Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. 否则,对于FrameWork,CMS或复杂的PHP应用程序编码,请始终使用require语句在执行流程中包括密钥文件。 This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing. 这将有助于避免损害应用程序的安全性和完整性,以防万一意外丢失一个密钥文件的情况。

Including files saves a lot of work. 包含文件可节省大量工作。 This means that you can create a standard header, footer, or menu file for all your web pages. 这意味着您可以为所有网页创建标准的页眉,页脚或菜单文件。 Then, when the header needs to be updated, you can only update the header include file. 然后,当标题需​​要更新时,您只能更新标题包含文件。

footer.php footer.php

<?php
echo "<p>Copyright &copy; 1999-" . date("Y") . " W3Schools.com</p>";
?>

index.php index.php

<!DOCTYPE html>
<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>

Live Preview 实时预览

Source from PHP 5 Include Files 来自PHP 5包含文件的

So your code looks like this 所以你的代码看起来像这样

if($a>$b)
{
  echo $a.' is greater';
}
include "if.php";
else 
{
  echo 'na';
}

Simply Its totally wrong. 简直是完全错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM