what does PHP die() return

39,475

Solution 1

In PHP the function die() just quit running the script and prints out the argument (if there's any).

http://php.net/die

Solution 2

Obviously, die() or its equivalent exit() don't return anything to the script itself; to be precise, this code doesn't make much sense:

if (die())) {
    echo 'are we dead yet?';
}

However, depending on what you pass as the (optional) argument of die() or exit(), it does return something to the caller, i.e. the command that caused your script to run. Its practical use is usually limited to the cli SAPI though, when you call the script from a command line using php /path/to/script.php.

Observe:

die('goodbye cruel world');

This code would print goodbye cruel world and then return an exit status code of 0, signalling to the caller that the process terminated normally.

Another example:

die(1);

When you pass an integer value instead of a string, nothing is printed and the exit status code will be 1, signalling to the caller that the process didn't terminate normally.

Lastly, die() without any arguments is the same as die(0).

The exit status of a process can be changed to signal different kinds of errors that may have occurred, e.g. 1 means general error, 2 means invalid username, etc.

Solution 3

Why don't you have a look at the wonderful documentation of PHP? It even contains information about die()

Solution 4

It is the same as exit() and according to documentation it returns nothing

Solution 5

It does not return. The script is terminated and nothing else is executed.

Share:
39,475
developer
Author by

developer

Updated on July 14, 2022

Comments

  • developer
    developer almost 2 years

    in PHP Does die() gives anything in return when we use it?

  • Alexander Garden
    Alexander Garden almost 12 years
    This is not quite accurate. If die() is called with an integer argument, it returns that value. In a web context, this may not mean much. In a CLI PHP script, that return value is meaningful; it's available for use in the shell.
  • SherylHohman
    SherylHohman about 4 years
    It prints $message before terminating the process. w3schools.com/PHP/func_misc_die.asp
  • SherylHohman
    SherylHohman about 4 years
    It prints the given message before terminating. w3schools.com/PHP/func_misc_die.asp