Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does PHP die() return

Tags:

php

die

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

like image 290
developer Avatar asked May 21 '09 10:05

developer


3 Answers

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

http://php.net/die

like image 61
Ivar Avatar answered Oct 05 '22 22:10

Ivar


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.

like image 20
Ja͢ck Avatar answered Oct 06 '22 00:10

Ja͢ck


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

like image 21
victor hugo Avatar answered Oct 06 '22 00:10

victor hugo