3

if ステートメントを 2 つの異なるファイルに除外することは何とか可能ですか? 次のコードのようなものを書くと、解析エラーが発生します: 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) { ?>

ボトム.inc.php:

<?php } ?>
4

4 に答える 4

3

いいえ、できません。各.phpファイルには、完全で有効な構文が必要です。includeファイルの作成は、コンテンツのコピーと貼り付けとまったく同じではありません。

于 2012-10-22T07:32:20.910 に答える
0

あなたはそれをすることはできません。あなたができることは次のようになります

あなたの中でtop.php

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

<?php } ?>
于 2012-10-22T07:32:14.723 に答える
0

各ファイルには正しい構文が必要です。最も簡単な方法は、ステートメントが true の場合に 1 という値を保持する変数を定義することです。

何かのようなもの:

top.php

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

index.php

if ($value == 1) // do something
于 2012-10-22T07:33:58.483 に答える
-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");
于 2012-10-22T07:31:44.347 に答える