Here's an example of my situation:
<?php
if(//condition)
{
//start out开发者_如何学编程put 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
?>
Basically, I'm loading the HTML code after the PHP code block into an output buffer. My conditional checks if the file that the output buffer writes to exists. If it exists, I just want to skip past the HTML and the output buffer writing, and start off at POINT A
. I would do an exit;
but I have more code I wish to output after POINT A
.
Any help?
- Enclose the code in conditional blocks.
- If doing 1. gives a too complicated structure consider using flags.
$doPrintSectionA = false;
And checking that flag before you print some section. - If you have PHP >= 5.3 you can use the
goto
statement.
Note that opening and closing PHP tags doesn't affect the control structures. Ex.:
<?php
if(rand(0,1)){
?>
<b>Hello World!</b>
<?php
}
?>
And a final warning:
<?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
?>
You certainly can "goto a:" in the script if you're using PHP 5.3 - http://us.php.net/manual/en/control-structures.goto.php
Use goto's and huge if statements though, and you'll end up with some pretty gnarly code. Here's what I would suggest for a start.
<?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
}
You might also want to organize your code into functions, possibly separate files and possibly even classes.
Actually though, if all you're doing is output static HTML into a file, you can skip the whole output-buffer-file-writing procedure and just create a separate static HTML file, full stop. Possibly use include 'file.html'
if you want to show it on this page as well.
精彩评论