PHP: What is the difference between exit(), die() and return; within "self" and included files?

11,111

Solution 1

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

Solution 2

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.

Solution 3

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); //
?>
Share:
11,111
Omar
Author by

Omar

I use SQL, MySQL, Oracle, php, javascript, Apache's httpd.conf VARIABLES and jQuery

Updated on June 05, 2022

Comments

  • Omar
    Omar almost 2 years

    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
    Omar over 11 years
    For what I read at 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."
  • Jordi Kroon
    Jordi Kroon over 11 years
    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.
  • Philip
    Philip over 3 years
    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.