Die and exit() in php

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?

Donald Duck

7,84120 gold badges70 silver badges91 bronze badges

asked Nov 25, 2009 at 6:28

1

There's no difference - they are the same.

PHP Manual for exit:

Note: This language construct is equivalent to die().

PHP Manual for die:

This language construct is equivalent to exit().

answered Nov 25, 2009 at 6:30

Marek KarbarzMarek Karbarz

28.6k6 gold badges51 silver badges73 bronze badges

16

DIFFERENCE IN ORIGIN

The difference between die() and exit() in PHP is their origin.

  • exit() is from exit() in C.
  • die() is from die in Perl.

FUNCTIONALLY EQUIVALENT

die() and exit() are equivalent functions.

PHP Manual

PHP Manual for die:

This language construct is equivalent to exit().

PHP Manual for exit:

Note: This language construct is equivalent to die().

PHP Manual for List of Function Aliases:

die is an alias for master function exit()


DIFFERENT IN OTHER LANGUAGES

die() and exit() are different in other languages but in PHP they are identical.

From Yet another PHP rant:

...As a C and Perl coder, I was ready to answer, "Why, exit() just bails off the program with a numeric exit status, while die() prints out the error message to stderr and exits with EXIT_FAILURE status." But then I remembered we're in messy-syntax-land of PHP.

In PHP, exit() and die() are identical.

The designers obviously thought "Hmm, let's borrow exit() from C. And Perl folks probably will like it if we take die() as is from Perl too. Oops! We have two exit functions now! Let's make it so that they both can take a string or integer as an argument and make them identical!"

The end result is that this didn't really make things any "easier", just more confusing. C and Perl coders will continue to use exit() to toss an integer exit value only, and die() to toss an error message and exit with a failure. Newbies and PHP-as-a-first-language people will probably wonder "umm, two exit functions, which one should I use?" The manual doesn't explain why there's exit() and die().

In general, PHP has a lot of weird redundancy like this - it tries to be friendly to people who come from different language backgrounds, but while doing so, it creates confusing redundancy.

answered Jan 8, 2015 at 23:13

Die and exit() in php

Geoffrey HaleGeoffrey Hale

9,7775 gold badges41 silver badges45 bronze badges

6

As stated before, these two commands produce the same parser token.

BUT

There is a small difference, and that is how long it takes the parser to return the token.

I haven't studied the PHP parser, but if it's a long list of functions starting with "d", and a shorter list starting with "e", then there must be a time penalty looking up the function name for functions starting with "e". And there may be other differences due to how the whole function name is checked.

I doubt it will be measurable unless you have a "perfect" environment dedicated to parsing PHP, and a lot of requests with different parameters. But there must be a difference, after all, PHP is an interpreted language.

Die and exit() in php

answered Nov 1, 2012 at 15:17

BobBob

8586 silver badges8 bronze badges

4

PHP manual on die:

die — Equivalent to exit

You can even do die; the same way as exit; - with or without parens.

The only advantage of choosing die() over exit(), might be the time you spare on typing an extra letter ;-)

Davicus

4184 silver badges14 bronze badges

answered Feb 19, 2014 at 14:17

Die and exit() in php

LeviteLevite

16.6k7 gold badges50 silver badges50 bronze badges

3

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

die():


exit():



Results:

die():

HTTP/1.1 304 Not Modified 
Connection: close

exit():

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

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

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

Die and exit() in php

answered May 2, 2018 at 17:09

EdwardEdward

2,2561 gold badge18 silver badges33 bronze badges

1

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...

answered Jul 15, 2015 at 14:00

LukasLukas

7977 silver badges13 bronze badges

1

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!' );

answered Oct 26, 2018 at 6:10

Die and exit() in php

aagjalpankajaagjalpankaj

1,0901 gold badge16 silver badges25 bronze badges

0

This output from https://3v4l.org demonstrates that die and exit are functionally identical.

Die and exit() in php

answered Jun 12, 2017 at 8:58

Die and exit() in php

Simon77Simon77

3464 silver badges4 bronze badges

This page says die is an alies of exit, so they are identical. But also explains that:

there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility. It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script.

So, call me paranoid, but there may be no dieing in the future.

answered Apr 22, 2014 at 4:20

Pedram BehrooziPedram Behroozi

2,3272 gold badges28 silver badges45 bronze badges

5

They are essentially the same, though this article suggest otherwise.

answered Nov 25, 2009 at 6:32

o.k.wo.k.w

25.1k6 gold badges64 silver badges62 bronze badges

2

Functionally, they are identical. So to choose which one to use is totally a personal preference. Semantically in English, they are different. Die sounds negative. When I have a function which returns JSON data to the client and terminate the program, it can be awful if I call this function jsonDie(), and it is more appropriate to call it jsonExit(). For that reason, I always use exit instead of die.

answered Jun 20, 2016 at 15:45

Die and exit() in php

Luo Jiong HuiLuo Jiong Hui

5,2692 gold badges23 silver badges17 bronze badges

4

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)

answered Jul 31, 2018 at 4:54

The result of exit() function and die() function is allways same. But as explained in alias manual page (http://php.net/manual/en/aliases.php), it says that die() function calls exit function. I think it is hard coded like below:

function die($msg){
  exit($msg);
}

This is not a performance issue for small, medium and large projects but if project has billions multiply billions multiply billions processes, this happens very important performance optimization state.

But very most of people don't thinks this is a problem, because if you have that much processes, you must think more problem than if a function is master or alias.

But, exact answer is that; allways master function is more faster than alias.

Finally; Alias manual page says that, you may no longer use die. It is only an alias, and it is deprecated.

It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script. This list is provided to help those who want to upgrade their old scripts to newer syntax.

answered Jan 17, 2018 at 15:49

MERT DOĞANMERT DOĞAN

2,61824 silver badges25 bronze badges

5

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?

answered Jul 27, 2017 at 13:48

noowienoowie

491 gold badge1 silver badge8 bronze badges

2

They sound about the same, however, the exit() also allows you to set the exit code of your PHP script.

Usually you don't really need this, but when writing console PHP scripts, you might want to check with for example Bash if the script completed everything in the right way.

Then you can use exit() and catch that later on. Die() however doesn't support that.

Die() always exists with code 0. So essentially a die() command does the following:


Which is the same as:


answered Nov 25, 2009 at 8:19

IchebIcheb

2371 silver badge1 bronze badge

4

What does die () do in PHP?

PHP
die() Function
1.
The die() function is an alias of the exit() function
2.
It takes a parameter as a Number
3.
It does not have any return value
4.
It is supported in PHP version 4+
PHP | die() & sleep() functions - GeeksforGeekswww.geeksforgeeks.org › php-die-sleep-functionsnull

Should I use die or exit in PHP?

There is no difference between die and exit, they are the same. "This language construct is equivalent to die()." "This language construct is equivalent to exit()."

What is the difference between exit and exit () in PHP?

There's no difference - they are the same. PHP Manual for exit : Note: This language construct is equivalent to die() .

What is difference between die and Echo?

What is the difference between echo and die() or are they same? The die() function can accept an optional argument string that it will output just before terminating the script. echo() only outputs its arguments, it will not terminate the script.