76

in PHP, how would one return from an included script back to the script where it had been included from?

IE:

1 - main script 2 - application 3 - included

Basically, I want to get back from 3 to 2, return() doesn't work.

Code in 2 - application

$page = "User Manager";
if($permission["13"] !=='1'){
    include("/home/radonsys/public_html/global/error/permerror.php");
    return();
}
bear
  • 11,364
  • 26
  • 77
  • 129
  • 1
    Make sure you are calling return() at the top-level scope in the included script (i.e. not in a function) – jscharf Aug 21 '09 at 20:58
  • What happens when you invoke return from your #3/included script? Can you post a sample of your code? – PTBNL Aug 21 '09 at 21:01
  • Sure: $page = "User Manager"; if($permission["13"] !=='1'){ include("/home/radonsys/public_html/global/error/permerror.php"); return(); } – bear Aug 21 '09 at 21:10
  • 5
    This is outlined in [the `include` documentation](http://www.php.net/manual/en/function.include.php) – Michael Berkowski Dec 18 '13 at 18:23

5 Answers5

153

includeme.php:

<?php
return 5;

main.php:

<?php
// ...
$myX = require 'includeme.php';
// ...

also gives the same result

This is one of those little-known features of PHP, but it can be kind of nice for setting up really simple config files.

See the PHP manual, especially Example #5 include and the return statement and its description.

hakre
  • 193,403
  • 52
  • 435
  • 836
Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
  • 40
    It should be noted that in most cases this would be considered to be a somewhat bad coding style, as it treats the include file as a function. A more 'standard' approach would be to put functions with a proper name in the include file, include the file (without any return statement outside of functions) and then call the needed function. – Henrik Opel Aug 21 '09 at 21:34
  • 13
    Take care when using parenthesis (i.e., include("x.php"); rather than include "x.php"), when it comes to return values. See Example 4 in the PHP documentation for `include`: http://php.net/manual/en/function.include.php – ravi Mar 21 '12 at 01:04
  • 1
    Excellent point. I usually try to avoid using parens that way, since they don't mean what most people think they mean in that context. e.g. I once saw a programmer struggle with: `include ('x.php') or die;` – Frank Farmer Mar 21 '12 at 01:08
  • 4
    @HenrikOpel, Explain and elaborate. *why* is it bad coding style? – Pacerier Aug 07 '13 at 11:28
  • 3
    @Pacerier, as said, because it treats the include file as a function, thus relying on a rather unusual, language specific and not very explicit behavior. Things like that can easily be missed by the fellow developer diving in for maintenance (or cost some time to understand what's going on), so if there is an equivalent, more common approach (like using explicit function calls in this case), it should be preferred. – Henrik Opel Sep 10 '13 at 11:53
  • 10
    @HenrikOpel That sounds like "Don't use all the features of PHP, because other people don't." Doesn't that set up a bad precedent for effectively removing features from a language, eventually boiling it down to the least common developer? – Umbrella Aug 12 '14 at 15:38
  • @HenrikOpel, i know this is old but i have a question regrading your recommendation. If we use the return value of the required file, is this required file stays in memory after it returns? If you require the file and then call the functions you will keep those function in memory even after you're done with it. no? – Louis Loudog Trottier Jan 29 '17 at 07:31
  • It does not treat the include file as a function. it is just including (or requiring). See as well [this answer to _How do I store an array in a file to access as an array later with PHP?_](https://stackoverflow.com/a/55421531/367456) for which you explicitly don't want to have a function (and therefore not using "include as a function"). This is static data. – hakre Jul 11 '23 at 21:23
26

return should work, as stated in the documentation.

If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call.

Lukas
  • 1,479
  • 8
  • 20
instanceof me
  • 38,520
  • 3
  • 31
  • 40
24

I know that this has already been answered, but I think I have a nice solution...

main.php

function test()
{
    $data = 'test';
    ob_start();
        include( dirname ( __FILE__ ) . '/included.php' );
    return ob_get_clean();
}

included.php

<h1>Test Content</h1>
<p>This is the data: <?php echo $data; ?></p>

I just included $data to show that you can still pass data to the included file, as well as return the included file.

Gravy
  • 12,264
  • 26
  • 124
  • 193
6

Hm, the PHP manual disagrees with you. return should bail you out of an included file. It won't work from within a function, of course.

Eevee
  • 47,412
  • 11
  • 95
  • 127
6

See Example #5 in the documentation for include.

You can get the return value of the script when including it, like:

$foo = include 'script.php';
Brian
  • 15,599
  • 4
  • 46
  • 63