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

7

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.

Rhnegative answered 28/4, 2011 at 21:3 Comment(8)
I really don't think this is something to care aboutLavolta
I consider it a bug that PHP allows you to do it more than one way. Don't worry about it.Conk
@Frits: It is not a bug, it is intentional behavior, similar to being able to use print or echoBarnsley
@Frits No, that isn't a bug by any reasonable definition.Abramabramo
A programming language shouldn't have multiple ways to express the exact same functionality. I know PHP has legacy issues and all that, I was just being philosophical.Conk
@Frits HAHA, you can't be serious. General purpose languages have uncountable ways of doing the same thing. That's kind of the point. I guess any language where 1 + 1 == 1 - -1 is full of bugs.Abramabramo
@Frits - good thing you're not a Perl programmer; they take pride in their "more than one way to do it" language.Correlation
Why does this question need protection? It hasn't had any answers deleted, let alone enough to justify preventing new users from answering it.Headreach
A
22

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.

Abramabramo answered 28/4, 2011 at 21:4 Comment(7)
Live fast, die young and leave a beautiful log file.Fidole
This could be a concern with the upcoming robot apocalypse. A quicker kill switch is always useful! =DClinquant
@meagar, well that begs another question. is it better to do echo then exit or echo then die?Rhnegative
@Neal Actually it raises another question. You're misusing the term "begs the question".Abramabramo
@meager. ok. what does begs the question mean vs raises? doesnt matter really, there is another question.Rhnegative
@meagar see the answer below, what about that?Rhnegative
@meagar, im referring to @Alix's answer, where you can return an exit code aside from 0 with dieRhnegative
M
1

no difference.

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

Mackinnon answered 28/4, 2011 at 21:5 Comment(1)
well one might be slow and painful, whereas the other is quick and easy, what do i know?Rhnegative
C
0

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.

Configurationism answered 28/4, 2011 at 21:34 Comment(4)
$query = mysql_query("SELECT * FROM tablename") OR exit(mysql_error()); works just as well. Die() and exit() are equivalent.Shut
@Neal This has nothing to do with die; you can write <statement#1> or <statement#2> with any series of statements, it isn't some feature of the language specific to die. Also commenting @meagar in an answer I haven't already commented on doesn't work.Abramabramo
Also there is no such thing as a "catch/die" sequence; this is simply plain old short-circuit evaluation.Abramabramo
pardon my simple terms then i suppose.Configurationism
D
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();
Derail answered 28/4, 2011 at 21:42 Comment(5)
i cant do die(42);? that wont do anything? what would be the result?Rhnegative
@Neal: I didn't said it wouldn't do anything, but it certainly won't be echo'ed! If you do die(42) the execution ends with the exit status of 42, this is mainly useful in CLI (Command Line Interface) environments. Just something to look for, since you may shot yourself in the foot if you're not careful (if you do mysql_query('...') or die(mysql_errno()) for instance).Derail
@Alix i was under the impression that only exit(42)does that and die always exits with 0Rhnegative
@Neal: No, they are aliases: die — Equivalent to exit(), see ideone.com/f7E0F. =)Derail
@Alix that is odd. did not know thatRhnegative
S
0

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.

Shut answered 28/4, 2011 at 22:4 Comment(0)
S
0

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!":"");
Sneaker answered 28/4, 2011 at 22:48 Comment(4)
huh? why can't you just do: die("The frobnusticator blew up!");Rhnegative
Because die output may get echo'd to a browser, which is fine in debug, but not in production. Yes, you'd ideally configure against this.Sneaker
You shouldn't really be dieing in production code anyways. die is like assert - by the time you've tested things well enough to roll code out to production, your code should be incapable of reaching a die statement.Abramabramo
Very true; however even the best code hits problems in production - you could hit a die because of a full disk, for example. But I wasn't trying to address die vs something else, just 2 forms of dieSneaker
G
0

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); //
?>
Garfieldgarfinkel answered 2/1, 2015 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.