There's no difference - they are the same.
PHP Manual for
exit
:Note: This language construct is equivalent todie()
.
PHP Manual for
die
:This language construct is equivalent toexit()
.
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 are 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.
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...
When using command line,
die("Error");
Will print to "Error" to STDOUT and exit with error code 0.
if you want to exit with error code 1, you have to:
fwrite(STDERR, "Error");
exit(1);
It could be useful while executing php scripts from command line or shell scripts and you want to see if the script terminated with a non zero exit code.
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.
0 comments:
Post a Comment