Posted by: phillipnb | March 21, 2012

Die() and Exit()


In a recent interview, an experienced php developer was asked the difference between die() and exit(). The gentleman was shocked but he did manage to give a convincing answer. There are plenty of articles on the Internet on this topic which says they are both the same and there are many others who argue for and against the notion that both die() and exit() are not the same.So, let us discuss and try to reach a conclusion on what exactly is the difference between die() and exit() in php.

According to the php manual from php.net, both die() and exit() are language constructs. The manual also goes on to say that die() is an alias of exit() and vice-versa. Honestly, there are not many differences between these two language constructs except for the the fact that die starts with a ‘d’ and exit starts with an ‘e’. To prove this let us play with a few examples.

for($i = 0; $i < 10; $i++)
{
    if ($i == 2)
    {
        exit("\n Using exit(), We are done");
    }
}

Now let us look at the same example using die();

for($i = 0; $i < 10; $i++)
{
    if ($i == 2)
    {
        die("\n Using die(), We are done");
    }
}

The output for each of these will be: “Using exit(), We are done Using die(), We are done” respectively. Now let us try each of these to output a number.

for($i = 0; $i < 10; $i++)
{
    if ($i == 2)
    {
        exit(-1);
    }
}

Looking at the output for the same example using die();

for($i = 0; $i < 10; $i++)
{
    if ($i == 2)
    {
        die(-1);
    }
}

These examples were edited using a textpad editor. The output for both the above cases were “Tool completed with exit code -1”.

So, the honest answer to the question “What is difference between die() and exit() in php” IS – There is not a single visible difference between these two functions. They both are the same, one is the alias of the other. If at all any one of you can find a real visible difference then I would appreciate if you could post it in the comments section of this blog.

Till next time, Happy PHP programming!

Advertisements

Responses

  1. No discernable difference, but there’s a difference in speed.

    Read this for benchmarks:
    http://obvioushints.blogspot.com/2008/09/diehello-world-vs-exithello-world-vs.htm

    Not my blog, but still valid info.

    • Thanks Don. Interesting benchmarks. A big surprise to me to know that “exit()” is far better than “echo()” and “die()”. Thanks again for sharing this information.

  2. The w3schools insist there is a difference–to say otherwise results in 5 points deducted from their sample php certification test. There is a difference, I have seen, in only one instance: IE totally ignores the exit() call after a redirect function, and to make it respond, the die() function is needed.

    • Rose, I have not tested exit() with I.E., so i am not in a position to comment. But thank you for the observation.

    • W3schools shoud not be relied upon.
      Here is a joke about it: w3fools.com

    • How can IE or any other browser ignore exit() or die() or do something about it? The browser doesn’t execute PHP, PHP is a server side language meaning the browser never sees a code of it. All the browser sees is the result of it, received from the server. The code is interpreted by the server then the result is sent to the browser. The browser can only display the code if you call it locally without a server and will display it the same as in a text editor!

      • Browser is a client and it doesn’t interfere with the code executing on the server. All that the server gives back to the client is a response irrespective of whether it contains an exit or anything else.

      • to phillipnb: I just realised I should have used the link in my email to reply to you.

      • Dan,
        OK, I was too fast, I didn’t realize that you were replying to another post and not asking a question – my mistake, forgive me.

      • Ah, thank you phillipnb, but nothing to forgive, as you said just a mistake, not deliberate.

      • ok.

  3. to phillipnb:
    If you read my reply above, you should understand I am saying exactly that. I was replying to Rose telling her because she said:
    “There is a difference, I have seen, in only one instance: IE totally ignores the exit() call after a redirect function, and to make it respond, the die() function is needed.”,

    and I wrote that a browser doesn’t receive any code, it just receives the result from the server. I should have written: Rose, how could you say…
    I guess I didn’t start my reply with the right words.
    According to your reply to her it is like you had not realised that there no test to be done about this with any browser as you wrote to her:
    “Rose, I have not tested exit() with I.E., so i am not in a position to comment. But thank you for the observation.”

    That’s why I had put my reply to her otherwise I wouldn’t have needed.

    Thank you phillipnd

  4. Dia() first terminate the script and then print message.
    Exit() first print message and then terminate the script.

  5. Die
    Print the string & variables
    Cannot stop the execution of the code

    Exit
    Cannot print the out put.
    Stop the execution of the code

    <?php
    $name="Reni Raj NR “;
    $location=”Kerala “;
    echo $name;
    die(“This line print by DIE.”.$location);
    exit(“This line print by EXIT”); //Cannot print
    echo $name.$location;
    ?>

  6. after die() function exit not call
    so output of exit not print
    if you comment to die() statement then exit also print….


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: