Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

include

php

exit

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?

like image 719
batfastad Avatar asked Jun 13 '11 17:06

batfastad


People also ask

What does exit () do in PHP?

The exit() function prints a message and terminates the current script.

How do you exit a PHP program?

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.


2 Answers

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.

like image 169
Pekka Avatar answered Oct 01 '22 12:10

Pekka


You are looking for return; command. This will terminate execution of only the child.php, and parent.php will be processed after that.

like image 24
Sandeep Kumar Avatar answered Oct 01 '22 11:10

Sandeep Kumar