Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences in die() and exit() in PHP?

Tags:

php

exit

die

What are the differences between die() and exit() functions in PHP?

I think both have the same functionality, but I doubt there is something different in both... what is it?

like image 694
coderex Avatar asked Nov 25 '09 06:11

coderex


People also ask

What is the difference between die () and exit () in PHP?

The exit() method is only used to exit the process. The die() function is used to print the message. The exit() method exits the script or it may be used to print alternate messages.

Do die () and exit () functions do the exact same thing?

There's no difference - they are the same. PHP Manual for exit : Note: This language construct is equivalent to die() .

What does exit () do in PHP?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.

What is the difference between ECHO and die?

What is the difference between echo and die() or are they same? The die() function can accept an optional argument string that it will output just before terminating the script. echo() only outputs its arguments, it will not terminate the script.


2 Answers

There's no difference - they are the same.

PHP Manual for exit:

Note: This language construct is equivalent to die().

PHP Manual for die:

This language construct is equivalent to exit().

like image 144
Marek Karbarz Avatar answered Sep 18 '22 00:09

Marek Karbarz


DIFFERENCE IN ORIGIN

The difference between die() and exit() in PHP is their origin.

  • exit() is from exit() in C.
  • die() is from die in Perl.

FUNCTIONALLY EQUIVALENT

die() and exit() are equivalent functions.

PHP Manual

PHP Manual for die:

This language construct is equivalent to exit().

PHP Manual for exit:

Note: This language construct is equivalent to die().

PHP Manual for List of Function Aliases:

die is an alias for master function exit()


DIFFERENT IN OTHER LANGUAGES

die() and exit() are different in other languages but in PHP they are identical.

From Yet another PHP rant:

...As a C and Perl coder, I was ready to answer, "Why, exit() just bails off the program with a numeric exit status, while die() prints out the error message to stderr and exits with EXIT_FAILURE status." But then I remembered we're in messy-syntax-land of PHP.

In PHP, exit() and die() are identical.

The designers obviously thought "Hmm, let's borrow exit() from C. And Perl folks probably will like it if we take die() as is from Perl too. Oops! We have two exit functions now! Let's make it so that they both can take a string or integer as an argument and make them identical!"

The end result is that this didn't really make things any "easier", just more confusing. C and Perl coders will continue to use exit() to toss an integer exit value only, and die() to toss an error message and exit with a failure. Newbies and PHP-as-a-first-language people will probably wonder "umm, two exit functions, which one should I use?" The manual doesn't explain why there's exit() and die().

In general, PHP has a lot of weird redundancy like this - it tries to be friendly to people who come from different language backgrounds, but while doing so, it creates confusing redundancy.

like image 21
Geoffrey Hale Avatar answered Sep 17 '22 00:09

Geoffrey Hale