tags:

views:

310

answers:

4

I'm trying to implement caching for a PHP script I'm writing, but I keep running into the following problem. I want the script to be included in other PHP pages, but when I try to pass the cached file and exit the embedded script it exits both the script and the parent page, but doesn't parse the rest of the code on the parent page. See the code below for an example.


index.php

<?php
  echo "Hello World!<br />";

  include("file2.php");

  echo "This line will not be printed";
?>


file2.php

<?php
  $whatever = true;

  if ($whatever == true) {
    echo "file2.php has been included<br />";
    exit; // This stops both scripts from further execution
  }

  // Additional code here
?>


If the above index.php is executed you get the following output:

Hello World! 
file2.php has been included

However, I'm trying to get it to look like this:

Hello World! 
file2.php has been included
This line will not be printed

Thanks in advanced to those who can help!

+2  A: 

Just wrap the "additional code here" in an else statement?

<?php
  $whatever = true;

  if ($whatever == true) {
    echo "file2.php has been included<br />";
  } else {
    // Additional code here
  }
?>

Otherwise I'm not sure what you're getting at. The exit command always terminates the current execution in whole - not just execution of the current file (for which, there is no command)

EDIT

Thanks to comments and posts by PHLAK, tomhaigh, MichaelM, and Mario, I myself learned something today - that you CAN indeed terminate the execution of a single included file w/the return command. Thanks, guys!

Peter Bailey
I guess this way will work, however, there would then be ~200 lines of code in the else statement.
PHLAK
+1  A: 

Why not encapsulate the contents of file2.php into a function. That way you can return from the function when you need to, and the rest of the execution will not halt. eg:

file2.php

<?php
    // this fuction contains the same code that was originally in file2.php
    function exe() 
    {
        $whatever = true;
        if ($whatever)
        {
            echo "file2.php has been included <br />";
            // instead of exit, we just return from the function
            return;
        }
     }

     // we call the function automatically when the file is included
     exe();
?>

Leave index.php exactly as it is and you should see the output you are trying to achieve.

MichaelM
The problem is that I'm using this to cache the output of my entire script, and the script is ~200 lines of code. While this method would work, it's not the most elegant way of doing so.
PHLAK
+3  A: 

Use return; instead of exit; in the included file - this will only halt execution of that script.

Note that you an also use this to return a value to the parent script e.g.

file1.php

<?php
echo 'parent script';
$val = include('file2.php'); //$val will equal 'value'
echo 'This will be printed';

file2.php

<?php
echo 'child script';
return 'value';
Tom Haigh
+1  A: 

I personally try to avoid if-else conditions where possible and use (not sure if there's a coined term for it but) early-exit intercepting conditions.

index.php

<?php
echo 'header';
include 'content.php';
echo 'footer';
?>

content.php

<?php
if ($cached)
{
    echo cached_version();
    return; // return is not just for functions, in php...
}

//proceed with echoing whatever you want to echo if there's no cached version.
...
...
?>
Mario