36

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?

batfastad
  • 1,943
  • 3
  • 27
  • 37
  • 4
    return "terminates" the current script; exit and die both – venimus Jun 13 '11 at 17:41
  • 4
    It is helpful to understand how require/include work to explain the answers below. When you require/include a script, the code in that script gets inserted into the current script as if it were written that way. require/include do not _execute_ anything; they simply pull code from an external file into the currently script. When the script is executed, the PHP engine doesn't care where the code came from. – George Cummins Jun 13 '11 at 17:48
  • 1
    Please reconsidder your answer selection. Mark the answer of "Pekka" as solution since Pheonixs answer is not matching. – Daniel Mar 26 '15 at 12:47

9 Answers9

73

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • If you `return` from within an include, won't that specifically *not* terminate parent.php and continue executing from after the `include` statement? – Darth Android Jun 13 '11 at 17:42
  • return will return all the way up through the parent tho wont it? unless the include is a function – Ascherer Jun 13 '11 at 17:46
  • 7
    @Ascherer nope, it will return to the spot where the `include` was started, and continue there – Pekka Jun 13 '11 at 17:47
  • Use 'return 0' ou 'return true'. Only 'return', if there is something more below the line it will try to return as a value. – Rodrigo Vieira Feb 11 '20 at 03:18
  • It's important to differentiate between php files and cpu processes. Including a script does not create a new process. It just extends or augments the code that is being run. Using return vs exit is about program execution, not about the underlying process. Ending the process is the result of the code terminating whether by running out of code to execute, or by hitting an exit statement (implicit vs explicit). – TomTerrific Jun 17 '22 at 19:16
15

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

Sandeep Kumar
  • 1,193
  • 13
  • 14
4

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

Ehsan Chavoshi
  • 681
  • 6
  • 10
3

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

You can use

die();

to end the furthur execution of any script at any point. Use of Die puts an end to the parent scripts as well.

die("End");

Will output "end".

Pheonix
  • 6,049
  • 6
  • 30
  • 48
  • 8
    exit does the same thing, btw – Ascherer Jun 13 '11 at 17:45
  • Yes, i should have been more specific. The argument for Exit() is status code (which i think is returned to the system), while argument of die() is printed to Output. – Pheonix Jun 13 '11 at 17:50
  • 4
    actually, `die()` is equivalent to `exit()`, http://www.php.net/manual/en/function.die.php exit will take an int or string – Ascherer Jun 13 '11 at 19:44
2

Actually an exit; line in your child.php will terminate current php process that means parent.php will be terminated well.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You can also terminate an script by throwing an exception and catch it in one of the parent scripts.

This way you can control with precission which scripts to terminate and where to continue in the stack of "include / require" files.

jbaylina
  • 4,408
  • 1
  • 30
  • 39
1

die and exit will both terminate without prejudice. It is an application level command which cannot be caught or undone.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

Using exit() will stop script execution and terminate the child and all parents.

Darth Android
  • 3,437
  • 18
  • 19
-1

You can try to throw an exception:

throw new Exception('An Error Ocurred');
Edgar Ortega
  • 1,672
  • 18
  • 22