问题描述
|
这是我的情况的一个示例:
<?PHP
if(//condition)
{
//start output buffer
}
else
{
//skip through file until POINT A
}
?>
<!-- some nice html,no PHP here -->
<?PHP
//close output buffer & write it to a file
//POINT A << SKIP TO HERE
?>
基本上,我是将PHP代码块之后的HTML代码加载到输出缓冲区中。我的条件检查输出缓冲区写入的文件是否存在。如果存在,我只想跳过HTML和输出缓冲区的编写,然后从ѭ1开始。我会做一个exit;
,但是我想在POINT A
之后输出更多代码。
有什么帮助吗?
解决方法
将代码放在条件块中。
如果执行1.给出的结构过于复杂,请考虑使用标志。
$doPrintSectionA = false;
并在打印某些部分之前检查该标志。
如果您的PHP> = 5.3,则可以使用goto
语句。
请注意,打开和关闭PHP标记不会影响控件结构。例如:
<?php
if(rand(0,1)){
?>
<b>Hello World!</b>
<?php
}
?>
最后警告:
,<?php
if(//condition)
{
//start output buffer
}
else
{
//skip through file until POINT A
?>
<!-- some nice html,no php here -->
<?php
//close output buffer & write it to a file
<?php } ?>
//POINT A << SKIP TO HERE
?>
,如果您使用的是PHP 5.3,那么您当然可以在脚本中“转到a:\”-http://us.php.net/manual/en/control-structures.goto.php
但是,使用goto和巨大的if语句,您将得到一些漂亮的粗糙代码。这是我建议的起点。
<?php
if(//condition)
{
//start output buffer
include \"content/page.html\";
//close output buffer & write it to a file
}
//POINT A
?>
,if (/* condition */) {
//start output buffer
<!-- some nice html,no php here -->
//close output buffer & write it to a file
}
您可能还希望将代码组织成函数,可能是单独的文件甚至可能是类。
但是实际上,如果您要做的只是将静态HTML输出到文件中,则可以跳过整个输出缓冲区文件编写过程,而只需创建一个单独的静态HTML文件即可。如果您也想在此页面上显示ѭ10,也可以使用。