包含后的PHP解析文件

时间:2012-08-03 15:50:11

标签: php

这就是我想要做的事情:

<? include 'header.php' ?>
some html
<? include 'footer.php' ?>
在header.php中

...

<? //bunch of code
if (something){
//some stuff here, but no close brace
?>
在footer.php中

....

<? } //close the if brace ?>

这可能吗?我一直在解析错误。

感谢。

1 个答案:

答案 0 :(得分:2)

不,你不能这样做。你的括号都需要在同一个文件中。

如果您想要有条件地包含HTML,请将您的逻辑放在include s header.php footer.php

的文件中
<?php include 'header.php' ?>
<?php if (/* something */): ?>
some html
<?php else : ?>
some other html
<?php endif; ?>
<?php include 'footer.php' ?>

另外,请勿使用ASP样式(<? ?>)短开标签。他们被弃用了。您仍然可以使用<?= ?>输出变量等。

Here's an interesting conversation on that topic.

And here's the notes from the PHP manual.