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

匆匆过客 提交于 2019-12-19 06:28:33

问题


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.


回答1:


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.




回答2:


no difference.

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




回答3:


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.




回答4:


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



回答5:


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.




回答6:


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!":"");



回答7:


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); //
?>


来源:https://stackoverflow.com/questions/5825002/what-is-the-difference-between-echoexit-die-and-dieexit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!