13

Am am still on a PHP learning curb. When terminating a script, what is the difference between exit(), die(); and return;?:

  1. within the same file (Single script file)
  2. Within the child of an include
  3. Within the parent of an include
Omar
  • 11,783
  • 21
  • 84
  • 114
  • exit and die are the exact same thing, and the answer to your question is here: http://stackoverflow.com/questions/3484050/php-exit-or-return-which-is-better – Supericy Dec 20 '12 at 08:56
  • It's been asked before. See the answer to this question: http://stackoverflow.com/questions/8490731/exit-die-return-false – tom Dec 20 '12 at 08:56
  • @Supericy Yes and no. No, because if you care to pay attention to my detailed points, I want to know the effect these have on includes as well as other things – Omar Dec 20 '12 at 09:03
  • @Supericy Essentially they might look the same, but I am asking different things – Omar Dec 20 '12 at 09:04
  • @Omar Die and exit are indentical, die is just an alias for exit. The link I gave you answers your question. Exit halts *all* execution. A global return will halt the current script and return to the calling script (if there is one). – Supericy Dec 20 '12 at 09:09
  • @Supericy So, what are the cause/effect & difference of adding it if I am in a 2, 3, for level of includes and anything in between? -I am sure that if I am coding to include php1 to include php2 to include php3, etc, and somewhere in a function/etc I add (die/exit/return) in either of the php files, there's a difference. One will halt everything all the way to the parent and one will only halt the current one, etc... – Omar Dec 20 '12 at 09:10
  • @Supericy Again, this question is more about its effect within DIFFERENT LEVELS of includes – Omar Dec 20 '12 at 09:17

3 Answers3

9

Return returns a value. This can be anything and is meant for functions.

What are the differences in die() and exit() in PHP?

http://php.net/manual/en/function.return.php

Community
  • 1
  • 1
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55
  • 1
    For what I read at http://php.net/manual/en/function.return.php, return can also halt the execution of an included script file: "...return will also end the execution of...script file." – Omar Dec 20 '12 at 09:00
  • Quote: return, on the other hand, ends a function call and returns to the caller. At the end of a program, return sets the status value that is returned to the OS; the program is going to exit no matter what. – Jordi Kroon Dec 20 '12 at 11:08
  • As of PHP 7.1.0, return statements without an argument trigger E_COMPILE_ERROR, unless the return type is void, in which case return statements with an argument trigger that error. – Philip Dec 17 '20 at 10:07
5

die and exit (equivalent functions)

Terminates execution of the script.

return

Returns program control to the calling module. Execution resumes at the statement 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.


die vs exit

The difference between die() and exit() in PHP is their origin.


PHP Manual

PHP Manual for die:

This language construct is equivalent to exit().

PHP Manual for exit:

Note: This language construct is equivalent to die().

PHP Manual for List of Function Aliases:

die is an alias for master function exit()


DIFFERENT IN OTHER LANGUAGES

die() and exit() are different in other languages but in PHP they are identical.

From Yet another PHP rant:

...As a C and Perl coder, I was ready to answer, "Why, exit() just bails off the program with a numeric exit status, while die() prints out the error message to stderr and exits with EXIT_FAILURE status." But then I remembered we're in messy-syntax-land of PHP.

In PHP, exit() and die() are identical.

The designers obviously thought "Hmm, let's borrow exit() from C. And Perl folks probably will like it if we take die() as is from Perl too. Oops! We have two exit functions now! Let's make it so that they both can take a string or integer as an argument and make them identical!"

The end result is that this didn't really make things any "easier", just more confusing. C and Perl coders will continue to use exit() to toss an integer exit value only, and die() to toss an error message and exit with a failure. Newbies and PHP-as-a-first-language people will probably wonder "umm, two exit functions, which one should I use?" The manual doesn't explain why there's exit() and die().

In general, PHP has a lot of weird redundancy like this - it tries to be friendly to people who come from different language backgrounds, but while doing so, it creates confusing redundancy.

Community
  • 1
  • 1
Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
2

Return is returns a value (char,int,string,array...) and exit from function.

From php manual :

Note: This language construct is equivalent to die().

But still there are difference between die and exit :

Using die() you can post a string : die("An error occurred");

Same result with using exit()

<?php
    echo("An error occurred <br>");
    exit(0);
?>

OR if you are cli or unix shell :

Using PHP on the command line, die("An error occurred") simply prints "An error occurred" to STDOUT and terminates the program with a normal exit code of 0.

<?php
    fwrite(STDERR, "An error occurred \n");
    exit(0); //
?>
EngineerCoder
  • 1,445
  • 15
  • 30