What are the differences in die() and exit() in PH

2019-01-02 19:08发布

What are the differences between die() and exit() functions in PHP?

I think both have the same functionality, but I doubt there is something different in both... what is it?

标签: php exit die
17条回答
素衣白纱
2楼-- · 2019-01-02 19:49

In w3schools Quiz: The die() and exit() functions do the exact same thing? my answer is false. That is incorrect answer. The right answer is true.

Here is the screenshot: enter image description here

查看更多
萌妹纸的霸气范
3楼-- · 2019-01-02 19:52

As all the other correct answers says, die and exit are identical/aliases.

Although I have a personal convention that when I want to end the execution of a script when it is expected and desired, I use exit;. And when I need to end the execution due to some problems (couldn't connect to db, can't write to file etc.), I use die("Something went wrong."); to "kill" the script.

When I use exit:

header( "Location: http://www.example.com/" ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit; // I would like to end now.

When I use die:

$data = file_get_contents( "file.txt" );
if( $data === false ) {
    die( "Failure." ); // I don't want to end, but I can't continue. Die, script! Die!
}
do_something_important( $data );

This way, when I see exit at some point in my code, I know that at this point I want to exit because the logic ends here. When I see die, I know that I'd like to continue execution, but I can't or shouldn't due to error in previous execution.

Of course this only works when working on a project alone. When there is more people nobody will prevent them to use die or exit where it does not fit my conventions...

查看更多
ら面具成の殇う
4楼-- · 2019-01-02 19:54

Something I have noticed in my scripts at least is that exit() will stop the currently executing script and pass control back to any calling script, while die will stop php in its tracks. I would say that is quite a big difference?

查看更多
流年柔荑漫光年
5楼-- · 2019-01-02 19:55

From what I know when I look at this question here

It said there that "in PHP, there is a distinct difference in Header output. In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter", and tested (personally)

查看更多
低头抚发
6楼-- · 2019-01-02 19:59

Here is something that's pretty interesting. Although exit() and die() are equivalent, exit() closes the connection. die() doesn't close the connection.

exit():

<?php
    header('HTTP/1.1 304 Not Modified');
    exit();
?>

die():

<?php
    header('HTTP/1.1 304 Not Modified');
    die();
?>

Results:

exit():

HTTP/1.1 304 Not Modified 
Connection: Keep-Alive 
Keep-Alive: timeout=5, max=100

die():

HTTP/1.1 304 Not Modified 
Connection: close

Just incase in need to take this into account for your project.

Credits: https://stackoverflow.com/a/20932511/4357238

查看更多
弹指情弦暗扣
7楼-- · 2019-01-02 20:00

Functionality-wise they are identical but I use them in the following scenarios to make code readable:

Use "die" when there is an error and have to stop the execution.

e.g. die( 'Oops! Something went wrong' );

Use "exit" when there is not an error and have to stop the execution.

e.g. exit( 'Request has been processed successfully!' );

查看更多
登录 后发表回答