What’s the difference between die and exit in PHP?

There is no difference between die and exit in PHP. Both exit and die are language constructs that output a message and terminate the current PHP script.

Why does PHP have two functions that do the same thing?

In certain cases, function names and language constructs are kept in order to provide backwards compatibility. These kind of functions are called aliases.

For example: The function sizeof does the exact same thing as count.

Think of it like an email forward. When an employee leaves a company, their email address is sometimes forwarded onto a colleague.

In the world of PHP, an older function is sometimes “forwarded” onto a newer and better function. For example: In PHP 7.1.0, the function rand became an alias of mt_rand. Why? Because mt_rand uses a better randomization algorithm and the algorithm behind the original rand function was considered to be flawed.

Why do both die and exit exist?

This goes back all of the way to the origins of PHP. PHP was heavily influenced by programming languages such as C and Perl.

In C, the exit() function is used to terminate the calling process. In Perl, the die function is used to raise an exception and exit the Perl process with a failure.

Hopefully, this article helped to clear up any confusion!