PHP exit() from within included script, exit parent script?

Viewed 22535

In PHP, if I use the include() or require() functions to start running code in another script, is there a way to terminate the parent script from within the child?

So say I have this in parent.php:
require('child.php');

And this in child.php:
exit();

Will that terminate just child.php, or parent.php as well?
Is there a way to terminate parent.php from within child.php, without adding any further code to parent.php?

It's for an elaborate custom 404 error page in PHP which detects whether it's been called by Apache using ErrorDocument, or whether a user has requested a page from our custom CMS which doesn't exist. If it's the latter, then I want to just require my 404.php (child) and output from that and not continue executing the parent.

Anyone know if it's possible to terminate a parent PHP script from within an included/required script?

9 Answers

You can use return if you need to exist included file but continue from main script.

return

(PHP 4, PHP 5, PHP 7)

return returns program control to the calling module.Execution resumes at the expression following the called module's invocation.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required,then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini,then that script file's execution is ended

Related