PHP - exit or die() in AJAX requests?
What's the best practice here? Using d开发者_JAVA技巧ie() or exit()? What's the difference between the two?
if($_GET['do_thing']):
echo 'bla bla';
exit(); // or die(), or something else?
endif;
die('bla bla');
, echo 'bla bla';exit();
and exit('bla bla');
do the same thing. Personally, I use die
only for debugging code, and an empty exit
for regular termination - as in your case. However, die
and exit
are synonyms, so it does not matter which one you use.
I use die() when making mysql queries to log the errors, I use exit to bail out of loops.
I prefer die, but for no particular reason. PHP Docs say that exit is the real language construct (http://www.php.net/manual/en/function.exit.php) and die is simply an alias, but both work. It just depends on your coding style and choice of grammar!
精彩评论