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']))
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?
php expression
Paulpro
source share