排除if语句?

时间:2012-10-22 07:27:53

标签: php if-statement include require

以某种方式可以将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 } ?>

4 个答案:

答案 0 :(得分:3)

不,这是不可能的。每个.php文件都需要具有完整,有效的语法。 include个文件与复制和粘贴其内容并不完全相同。

答案 1 :(得分:0)

你做不到。您可以做的是以下

top.php

<?php if($condition) { 
           include "bottom.inc.php";    
?>

<?php } ?>

答案 2 :(得分:0)

每个文件都需要具有正确的语法。最简单的方法是定义一个变量来保存一个值,如果该语句为真,则该值为1。

类似的东西:

top.php

if (1 == 1) $value = 1;

的index.php

if ($value == 1) // do something

答案 3 :(得分:-3)

<?
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");
相关问题