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?
The exit() function prints a message and terminates the current script.
The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.
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.
You are looking for return
; command. This will terminate execution of only the child.php, and parent.php will be processed after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With