AKor
AKor

Reputation: 8882

PHP skip through file under certain conditions

Here's an example of my situation:

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

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?

Upvotes: 0

Views: 2634

Answers (4)

deceze
deceze

Reputation: 522076

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.

Upvotes: 1

Alin P.
Alin P.

Reputation: 44346

  1. Enclose the code in conditional blocks.
  2. If doing 1. gives a too complicated structure consider using flags. $doPrintSectionA = false; And checking that flag before you print some section.
  3. 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:

enter image description here

Upvotes: 6

AndrewR
AndrewR

Reputation: 6748

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

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157870

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

Upvotes: 1

Related Questions