Alex Rivera | Logout

What is the difference between echo('exit'); die; and die('exit');?

Asked 2011-04-28T21:03:47.527
10

I have seen some code do this:

if(something){
    echo 'exit from program';
    die;
}
...more code

And others that just use die:

if(something)   die('exit from program');
...more code

Is there any inherent difference in when it would end the program, should I be aware of the code that comes after it? etcetera

UPDATE

I am asking mainly, if it is a coding style, or if there is a real reason why some is coded one way versus another. I am not asking what the difference between exit and die is.

Edit
Report

1 Answer

0

For the specific example you posted they are equal, since the $status is a string, but as the manual says this may not always be the case:

If status is a string, this function prints the status just before exiting.

If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

So if instead of 'exit from program' you wanted to output, say 42 you would really need to do:

echo 42; die();
answered 2011-04-28T21:42:25.630

Your Answer