Difference between 'die' and 'exit'
Solution 1
According to Die it is Equivalent to exit. So yes, you can interchange them .
Solution 2
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.
That is one difference I could think of.
P.S. Above info obtained from php.net/exit
Solution 3
There is no difference between die() and exit() function. They both are same and worked same.
Again question is why php keep the both functions if they are same. Both functions are alias of each other function.
Due to API and keeping the backward compatibility both functions are kept.
Here is one more example:
is_int() and is_integer() are also same.
There are quite a few functions in PHP which you can call with more than one name. In some cases there is no preferred name among the multiple ones, is_int() and is_integer() are equally good for example. However 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. This list is provided to help those who want to upgrade their old scripts to newer syntax.
Full list of Aliases function you will find on following URL:
http://php.net/manual/en/aliases.php
May this will help you :)
Solution 4
die
is alias of exit
function.
There are many function aliases in php, due to how the language has evolved, evolve and get over it as well - http://www.php.net/manual/en/aliases.php.
Solution 5
die prints argument to STDOUT, not to STDERR (grep or 2>/dev/null will help you to test it) die returns to shell exit code as 0, but exit can return other code lets define die full analog in PHP:
function mydie($str){
echo $str.PHP_EOL;
exit(0);
}
Yogesh Suthar
SOreadytohelp Working in Nykaa as Tech Lead(iOS). I love to work in Swift for iOS. For cookies :- Disable Trace , httpOnly , Use SSL Cookies , Signed origin Contact email-id : yogesh[dot]mistry89[at]gmail[dot]com
Updated on June 05, 2022Comments
-
Yogesh Suthar almost 2 years
Possible Duplicate:
what are the differences in die() and exit() in PHP?I am totally confused in the difference of
die
andexit
.Most programmers use
die
like this.$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); //don't see mysql_* problem it is just example if (!$link) { die('Could not connect: ' . mysql_error()); }
and using
exit
like this$filename = '/path/to/data-file'; $file = fopen($filename, 'r') or exit("unable to open file ($filename)");
According to there functionality , I don't think so there is any difference because both terminates the execution of the script.
My question is
1) Can I interchange die with exit and vice-versa in these examples?
2) And the difference between these also.
Cheers...
-
Peon over 11 yearsAs @AdamPlocher said, they are one and the same. Use witch one you like best.
-
eis over 11 yearsI've actually only seen them used other way around... "or die(..)" etc :) but like said, there's no difference
-
GAURAV MAHALE almost 11 yearsmysql functions will be deprecated, please use PDO or mysqli;
-
-
Yogesh Suthar over 11 yearsthanks for answering . But can you tell me why
PHP
lang. developers created these two function. We can useexit
everywhere. -
asprin over 11 yearsMaybe the die function call was created to make Perl programmers feel at home
-
eis over 11 years@YogeshSuthar PHP is filled with aliases like that. Some of it is planned, but I would claim most of it is not. It's just been evolving and changing as time goes by and some turn out to be worse ideas than others.
-
Yogesh Suthar over 11 years+1 for finding at-least 1 difference.
-
Admin over 11 yearsThe
die
alias also makes for fun lines of code likecomply() or die();
-
MatBailie over 11 yearsIf you are citing deprecated aliases, please include examples.
-
Prasanth over 11 years@darvids0n
enter_the_dragon() or exit();
;) -
ThiefMaster over 11 yearsIt's still not a difference between the two functions. It's just that usually people use
die
with a message andexit
with an exit code. -
nssmart over 5 years+1 to ThiefMaster. You can change the last line to
die(1);
without any changes to the behavior.