排除if语句?
Is it somehow possible to exclude an if statement to two different files? When I write something like the following code, it gives a parse error: syntax error, unexpected end of file
. Apparently, i always have to close an if statement in the same included file. Is there a way to accomplish what i want?
index.php:
<?php
require "top.inc.php"; // opening the if statement
some code to execute if statement in top.inc.php is true
require "bottom.inc.php"; // closing the if statement
?>
top.inc.php:
<?php if (1 == 1) { ?>
bottom.inc.php:
<?php } ?>
以某种方式可以将if语句排除到两个不同的文件中吗? 当我编写类似下面的代码时,它会给出一个解析错误: index.php: p>
top.inc.php: p>
bottom.inc.php: p>
语法错误,意外的文件结尾 code>。 显然,我总是要在同一个包含文件中关闭if语句。 有没有办法实现我想要的? p>
&lt;?php
require“top.inc。 PHP的“; //打开if语句
如果top.inc.php中的语句为true,则执行一些代码
require“bottom.inc.php”; //关闭if语句
?&gt;
code> pre>
&lt; ?php if(1 == 1){?&gt;
code> pre>
&lt;?php}?&gt;
code> pre>
div>
No, it is not possible. Each .php file needs to have complete, valid syntax. include
ing files is not exactly the same as copy-and-pasting their content.
<?
file_put_contents("temp.php",file_get_contents("top.inc.php"));
$Middle_Code = <<<HEREDOC
/*
Other code to be put inside this heredoc,
but, of course, outside this comment block!
*/
HEREDOC;
file_put_contents("temp.php",$Middle_Code,FILE_APPEND);
file_put_contents("temp.php",file_get_contents("bottom.inc.php"),FILE_APPEND);
require("temp.php");
You can't do that. What you can do would be the following
in your top.php
<?php if($condition) {
include "bottom.inc.php";
?>
<?php } ?>
Each file will need to have correct syntax. The easiest way would be to define a variable to hold a value that is say 1 if the statement is true.
Something like:
top.php
if (1 == 1) $value = 1;
index.php
if ($value == 1) // do something