Is this special exit and death procedure documented in PHP?

I just read the Expressions page in PHP docs, and at the top of the page she wrote:

The simplest but most accurate way to define an expression is "all that matters."

This simple definition includes all the functions and most language constructs, however there are several language constructs that clearly indicate that they do not return a value.

Here is a list of language constructs that return a value:

Here are some interesting examples that do not return a value and therefore are not expressions:

I find die and exit particular interest because they can be used as expressions in PHP, despite the lack of return values. The following lines of code generate a syntax error, as expected:

 echo 'Hi' or echo 'Bye'; if(echo('foo')) return return(1); $foo['bar'] = isset($foo['bar']) ? unset($foo['bar']) : 0; if(unset($foo['bar'])) __halt_compiler() or die; 

However, the following PHP code does not contain any syntax errors:

 print 'Hi' or print 'Bye'; // Makes sense, print returns a value if(!die() and exit) // Wait what happening here? quit(die(exit(quit()))); // die and exit don't have return values (does quit?) $x = true ? die/2 : 5*exit(); $y = pow(die,7); isset($_GET['bar']) or die(); // This one is actually pretty commonly used. function quit(){ return exit; } 

I looked through the PHP docs and can't find any mention of this special call to die () and exit (). Do any PHP experts know if this is documented somewhere. Is this the intended behavior and is a safe pattern isset($_GET['bar']) or die(); ; could it suddenly break in a future version of PHP?

+8
php expression
source share
4 answers

die and exit (they share the T_EXIT token) fall under expr_without_variable rules during the parsing phase, so PHP is glad to have them in the context of expressions without a syntax error.

Do any PHP experts know if this is documented somewhere.

The PHP manual does not describe special treatment, but the first example on the exit manual page shows that it is used as … or exit .

Is this the intended behavior and safe pattern isset($_GET['bar']) or die(); ; could it suddenly break in a future version of PHP?

Yes. Yes. Everything possible, as if unlikely.

+3
source share

PHP does not detect errors, except at runtime, when the code path is reached. Unlike many other languages, it does not list errors when the page is "compiled", so you will see errors only if their corresponding lines of source code are executed.

In your example, evaluating the return value of exit or die never performed. PHP does not report an error because it never tried to evaluate the result in the first place, because the stream exited.

+4
source share

The wild assumption is that die and exit return a value, however it will never be returned, since execution is paused until it returns.

This can be implemented to add some "utility" to the die and exit functions.

+1
source share

die and exit return value, however this value should be used by the program that called the PHP script. This comment describes the behavior.

+1
source share

All Articles