PHP: die() Vs exit() – It has no difference but a difference

Overview

We are all at starting phase of learning, PHP has a BIG question on what is the difference between die() and exit(). The excitement of preparing a code with both statements, searching for answers and getting to a result is heart whelming.

It’s a general tendency to use exit() for testing/stopping execution and die() for checking database connection/operations.

Let’s compare it both… A step by step explanation to die Vs exit

PHP Language Constructs comparison “exit” Vs “die”:

NOTE: Built-in Functions are slower compared to Language Constructs, Language Constructs does not need parenthesis

Example 1:

Test exit:

Test die:

Example 2:

Test exit:

Test die:

As mysql_connect is deprecated from PHP 5.5 and is obsolete on PHP 7 you can try with mysqli_connect. Both (exit and die) can be used without parenthesis.

Example 3:

Test exit:

Test die:

The conclusion comes that both works SAME – there is no functional level differences with both.

If you check on php.net for die and exit definitions it’s mentioned:

exit — Output a message and terminate the current script

die — Equivalent to exit

But still there is a little or no difference – server need to read 4 characters for exit whereas server need to read 3 characters for die. For micro time difference die will execute faster then exit.

These are called “Aliases” – different name but same functional execution.

There are many more function aliases – you can get a grip from http://php.net/manual/en/aliases.php.

I insist to use die instead of exit on regular basis. Ultimately it’s on developer to choose on what to use, there is lot more than “Hello World” to explore.

mm

Kirtan Gajjar Head of Web Development Department

Kirtan Gajjar is Head of Web Development Department at Yudiz Solutions Pvt. Ltd. - A leading web development company. He has 7+ years of experience in various web development technologies. He would like to term himself as "Campaigner" who works in an organised and active way towards a goal, keeping all together. If he is asked to have a virtual power some day he would ask for "Power to Unite".

Comments are closed.

Top