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 } ?>
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