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

There is no difference between “die” and “exit” in PHP.

These two language constructs do the exact same thing.

Both “exit” and “die” will output a message and then kill the current PHP script.

For example, if you look at the PHP manual for “die”, you will see the following:

exit vs die php

A screenshot from the PHP manual.

This is kind of like using the word “tap” or “faucet”. Both of these words mean the exact same thing.

Why does PHP have “die” and “exit” if there are no differences between them?

This goes back to the origins of PHP.

You see, PHP was heavily influenced by two programming languages called “C” and “Perl”.

In C, the exit() function terminates the calling process. In Perl, the die function raises an exception and exits the process with a failure.

To make things easier for programmers from C and Perl, the creators of PHP decided to incorporate both “exit” and “die” into their language.

Basically, they were trying to make their language more welcoming.

Aliases.

In certain cases, function names and language constructs can share the exact same name.

In the world of computing, we call them 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, that company will often forward their emails to one of their colleagues.

The developers behind PHP will sometimes “forward” older functions onto newer and better alternatives.

Aliases allow programmers to use whichever name they are comfortable with.

They also give the developers of PHP the ability to remove older functions without breaking things.

For example, in PHP 7.1.0, the function rand became an alias of mt_rand. This is because mt_rand uses a better randomization algorithm.

The algorithm behind the original rand function is flawed. As a result, the PHP team decided to turn it into an alias of mt_rand.

That way, they were able to remove rand without actually removing it.