是否有可能将IF语句排除到两个不同的文件?当我编写类似以下代码的内容时,它会产生解析错误:syntax error, unexpected end of file
。显然,我总是必须在同一文件中关闭IF语句。有没有办法完成我想要的?
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 } ?>
不,这是不可能的。每个.php文件都需要具有完整的有效语法。include
ING文件与复制和按下其内容并不完全相同。
您不能做到这一点。您能做的就是以下
在您的top.php
<?php if($condition) {
include "bottom.inc.php";
?>
<?php } ?>
每个文件都需要具有正确的语法。最简单的方法是定义一个变量以保持一个值为1的值。
。类似:
top.php
if (1 == 1) $value = 1;
index.php
if ($value == 1) // do something
<?
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");