Question

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.

Was it helpful?

Solution

No, there is no difference; they will both write "exit" to STDOUT and terminate the program.

I would prefer the die("exit") method as it's less typing, easier to comment out and semantically clearer.

As far as "speed", why would you care which is faster? Do you need your program to die really quickly?

RE: Your update

... any inherent difference in when it would end the program ...

There is no difference, inherent or otherwise. They're identical. The second option, die('exit'), is a single statement, and so requires no braces when used with an if statement; this has nothing to do with the die and everything to do with blocks and flow control in C-style languages.

RE: Your comment/second update

Which way you die is a matter of personal preference. As I said, they are identical. I would choose the 2nd option for the reasons listed above: Shorter, clearer, cleaner, which amounts to "better" in my opinion.

The difference between exit and die is that exit allows you to return a non-zero status, while die returns 0. Neither function is "better", they serve different purposes.

OTHER TIPS

no difference.

And why asking for speed difference since you're dieing.

There IS a difference guys. DIE() can be used with other failable functions whereas echoing would need to caught as an error or exception.

$query = mysql_query("SELECT * FROM tablename") OR DIE(mysql_error());

Gives you an immediate catch/die sequence.

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();

The language constructs exit() and die() are equivalent, at least according to the PHP manual. I use exit() when that line should be reached and I want the script to stop at that point. Die() on the other hand is for situations that should not occur. That's just what feels most natural for me, you don't have to agree.

Mostly it's coding style. However, if you are outputting debug messages, echo then die is better:

echo "The frobnuticator blew up!";
die;

becomes

//echo "The frobnusticator blew up!";
die;

Of course, you'd most likely have

if ($debug) echo "The frobnusticator blew up!";
die;

Which is much easier on (my|the) eye than

die($debug?"The frobnusticator blew up!":"");

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); //
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top