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

batfastad picture batfastad · Jun 13, 2011 · Viewed 20.2k times · Source

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?

Answer

Pekka picture Pekka · Jun 13, 2011

exit(); Will that terminate just child.php, or parent.php as well?

It will terminate the entire script.

If you don't want to do that, but return to the including script, you can return from within an include.