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

21,858

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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".

Solution 5

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

Share:
21,858
batfastad
Author by

batfastad

Updated on November 03, 2020

Comments

  • batfastad
    batfastad over 3 years

    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?

  • Darth Android
    Darth Android almost 13 years
    If you return from within an include, won't that specifically not terminate parent.php and continue executing from after the include statement?
  • Ascherer
    Ascherer almost 13 years
    exit does the same thing, btw
  • Ascherer
    Ascherer almost 13 years
    return will return all the way up through the parent tho wont it? unless the include is a function
  • Pekka
    Pekka almost 13 years
    @Ascherer nope, it will return to the spot where the include was started, and continue there
  • Pheonix
    Pheonix almost 13 years
    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.
  • Ascherer
    Ascherer almost 13 years
    actually, die() is equivalent to exit(), php.net/manual/en/function.die.php exit will take an int or string
  • Rodrigo Vieira
    Rodrigo Vieira over 4 years
    Use 'return 0' ou 'return true'. Only 'return', if there is something more below the line it will try to return as a value.
  • TomTerrific
    TomTerrific almost 2 years
    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).