PHP: What is the difference between exit(), die() and return; within self and included files?
Return returns a value. This can be anything and is meant for functions.
What are the differences in die() and exit() in PHP?
http://php.net/manual/en/function.return.php
Difference between 'die' and 'exit'
According to Die it is Equivalent to exit. So yes, you can interchange them .
What is the difference between echo('exit'); die; and die('exit');?
No, there is no difference; they will both write "exit"
to STDOUT and terminate the program.
I would prefer the die("exit")
method as it's less typing, easier to comment out and semantically clearer.
As far as "speed", why would you care which is faster? Do you need your program to die really quickly?
RE: Your update
... any inherent difference in when it would end the program ...
There is no difference, inherent or otherwise. They're identical. The second option, die('exit')
, is a single statement, and so requires no braces when used with an if
statement; this has nothing to do with the die
and everything to do with blocks and flow control in C-style languages.
RE: Your comment/second update
Which way you die
is a matter of personal preference. As I said, they are identical. I would choose the 2nd option for the reasons listed above: Shorter, clearer, cleaner, which amounts to "better" in my opinion.
The difference between exit
and die
is that exit
allows you to return a non-zero status, while die
returns 0. Neither function is "better", they serve different purposes.
What is the difference between die() and exit()?
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()
.
exit(); die(); return false;
die()
and exit()
are precisely identical; they halt the entire PHP program and return to the OS. They're two different names for the same function.
return
, on the other hand, ends a function call and returns to the caller. At the end of a program, return
sets the status value that is returned to the OS; the program is going to exit no matter what.
What is PHP exit()/die() equivalent in Node.js
process.exit()
is the equivalent call.
PHP: Utilizing exit(); or die(); after header(Location: );
I have been looking for an answer on this as well. What I found:
Why die() or exit():
If you don't put a die() or exit() after your header('Location: http://something')
your script may continue resulting in unexpected behaviour. This may for example result in content being disclosed that you actually wanted to prevent with the redirect (HTTP 301). The aforementioned may not directly be visible for an end user as the browser may not render it (due to the 301). Conclusion, the exit() and die() functions stop the script from continuing.
Difference:
I also wanted to know the difference between the functions as it seems there is none. However, in PHP, there is a distinct difference in Header output.
In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter.
Exit() in action
<?php
header('HTTP/1.1 304 Not Modified');
exit();
?>
Results in:
HTTP/1.1 304 Not Modified
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Die() in action
<?php
header('HTTP/1.1 304 Not Modified');
die();
?>
Results in:
HTTP/1.1 304 Not Modified
Connection: close
Difference
So, die() closes the connection and exit() doesn't. It depends on performance whether or not you want to keep the connection open or close it. Both have advantages and disadvantages and depends on your specific requirement(s).
HTTP persistent connections on Wiki
Related Topics
Is There a PHP Function Like Python'S Zip
Truncate a Multibyte String to N Chars
PHPdoc Type Hinting For Array of Objects
How Do PHP Sessions Work? (Not "How Are They Used")
PHP, Get File Name Without File Extension
Remove Excess Whitespace from Within a String
Upload Photo to Album With Facebook'S Graph API
PHP.Ini & Smtp= - How to Pass Username & Password
Pdo Binding Values For MySQL in Statement
A Non Well Formed Numeric Value Encountered
I Have an Array of Integers, How to Use Each One in a MySQL Query (In PHP)
Convert Dot Syntax Like "This.That.Other" to Multi-Dimensional Array in PHP
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, Null Given In
Understanding the "Post/Redirect/Get" Pattern
How to Catch the Fatal Error: Maximum Execution Time of 30 Seconds Exceeded in PHP