tags:

views:

221

answers:

4

Hello, 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();
}
+5  A: 

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.

streetpc
+3  A: 

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
+2  A: 

includeme.php:

$x = 5;
return $x;

main.php:

$myX = include('includeme.php');

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

Frank Farmer
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
+2  A: 

Basically you don't (that is, not explicitly ;)

If you include a script via include(), it is treated as if the exact code of the script would be written in the place of the include statement. After that, the calling script simply continues.

Henrik Opel