Is there any differences between PHP die() and exit()?
Based on the documentation, the PHP die() function is equal to the exit() function.
There’s no technical difference between the die() and exit() function.
There is only a semantic difference between the die() and exit() function:
die()is recommended when you stop PHP script because of an exceptionexit()is used when your PHP script require something from the user
Here’s an example:
// ๐ Using exit
$name = null;
if (!$name) {
echo "Please input name";
exit();
}
// ๐ Using die
$name = null;
if (!$name) {
die("name variable is empty!");
}
While both exit and die do the same thing, die feels like there’s an error with the program, while exit seems like the program needs something from the user.
And that’s the only difference between die() and exit() function.
The guideline is: use exit() when the user needs to do something. Use die() when some expected data is missing.
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022