PHLAK
PHLAK

Reputation: 22497

Quiting a PHP script within another PHP page

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

Upvotes: 1

Views: 535

Answers (4)

MichaelM
MichaelM

Reputation: 5818

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 function 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.

Upvotes: 1

Peter Bailey
Peter Bailey

Reputation: 105868

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!

Upvotes: 2

Mario
Mario

Reputation: 1525

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

Upvotes: 1

Tom Haigh
Tom Haigh

Reputation: 57815

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';

Upvotes: 3

Related Questions