Die and exit() in php
What are the differences between Show 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
PHP
Manual for
answered Nov 25, 2009 at 6:30
Marek KarbarzMarek Karbarz 28.6k6 gold badges51 silver badges73 bronze badges 16 DIFFERENCE IN ORIGINThe difference between
FUNCTIONALLY EQUIVALENT
PHP ManualPHP Manual for
PHP Manual for
PHP Manual for List of Function Aliases:
DIFFERENT IN OTHER LANGUAGES
From Yet another PHP rant:
answered Jan 8, 2015 at 23:13
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.
answered Nov 1, 2012 at 15:17
BobBob 8586 silver badges8 bronze badges 4 PHP manual on die:
You can even do The only advantage of choosing
Davicus 4184 silver badges14 bronze badges answered Feb 19, 2014 at 14:17
LeviteLevite 16.6k7 gold badges50 silver badges50 bronze badges 3 Here is something that's pretty interesting. Although
Results:
Just incase in need to take this into account for your project. Credits: https://stackoverflow.com/a/20932511/4357238
answered May 2, 2018 at 17:09
EdwardEdward 2,2561 gold badge18 silver badges33 bronze badges 1 As all the other correct answers says, 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 When I use exit:
When I use die:
This way, when I see Of course this only works when working on a project alone. When there is more people nobody will prevent them to use 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. Use exit() when there is not an error and have to stop the execution. e.g. answered Oct 26, 2018 at 6:10
aagjalpankajaagjalpankaj 1,0901 gold badge16 silver badges25 bronze badges 0 This output from https://3v4l.org demonstrates that die and exit are functionally identical. answered Jun 12, 2017 at 8:58
Simon77Simon77 3464 silver badges4 bronze badges This page says
So, call me paranoid, but there may be no 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
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:
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.
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
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.
|