8

I've just read the page on Expressions in the PHP docs, and right at the top it says:

The simplest yet most accurate way to define an expression is "anything that has a value".

That simple definition includes all functions and most language constructs, however there a few language constructs that explicitly state they do not return a value.

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

Here are the interesting few which do not return a value, and therefore are not expressions:

I find die and exit of particular interest, because they can be used as expressions in PHP despite having no return values. The following lines of code all throw 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 is completely free of syntax errors:

print 'Hi' or print 'Bye';    // Makes sense, print returns a value

if(!die() and exit)           // Wait what's 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've looked through the PHP docs and can't find any mention of this special treatment of die() and exit(). Do any PHP experts know if this is documented anywhere. Is this intended behaviour, and is the isset($_GET['bar']) or die(); pattern safe to use; could it suddenly break in a future version of PHP?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Paul
  • 139,544
  • 27
  • 275
  • 264
  • 8
    Isn't this just a matter of semantics? Is it that `die()`/`exit()` don't have a return value *because they never return*? It's not that they are *syntactically* not expressions, but that at runtime you'll never get to see the return value because your program never gets a chance to use it. – Greg Hewgill May 07 '12 at 02:35
  • 2
    safe to use? yes. that idiom is littered all over the manual in the examples. They couldn't back out on it. Also, on the internals list, they always talk about how many scripts in the wild would be broken by a change before making it, and this would break...im guessing 1/3 of all php scripts? lol – goat May 07 '12 at 03:05

4 Answers4

4

PHP does not detect errors except at run time when the code path is reached. Unlike many other languages, it does not list the errors when the page is "compiled" - so you'll only see errors as their respective lines of source code are executed.

In your example, the evaluation of the return value of exit or die is never done. PHP doesn't report an error because it never tried to evaluate the result in the first place, because the thread exited.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
3

die and exit (they share the T_EXIT token) fall under the rules for expr_without_variable during the parsing phase, which is why PHP is happy to have them in an expression context without giving a syntax error.

Do any PHP experts know if this is documented anywhere.

There is no description of the special treatment in the PHP manual, however the first example on the exit manual page shows it being used as … or exit.

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

Yes. Yes. Anything's possible, however unlikely.

salathe
  • 51,324
  • 12
  • 104
  • 132
  • 1
    @salathe It is possible, but your reason is wrong. It is possible, because 100% backwards compatibility is not one of PHP's design goals: http://www.php.net/manual/en/migration53.incompatible.php – Pacerier Jun 12 '12 at 08:06
1

A wild guess is that die and exit do return a value, however, it will never be returned since the execution is halted before it is returned.

This might have been implemented in order to add some "usefulness" to the die and exit functions.

Ben
  • 16,275
  • 9
  • 45
  • 63
  • die and exit stop execution at run-time. The syntax error should be caught at compile time. – Paul May 07 '12 at 02:44
  • @PaulP.R.O. Perhaps they thought it'll be useful, which it is. They probably return a value, like `print`, however, the value is never returned since the execution is halted beforehand. – Ben May 07 '12 at 02:53
  • The documentation explicitly states that they do not have a return value: http://php.net/manual/en/function.exit.php . I agree that it is useful, but if it is not documented anywhere then I'd be very hesitant to use the `or die();` notation when according to the docs it seems to be a syntax error. – Paul May 07 '12 at 03:05
  • @PaulP.R.O. They don't return any value *per se*, since the returned value will **never** get returned. Code wise, they might return something in order to avoid raising a syntactic error. Maybe tomorrow I'll download PHP's source code and look for these functions. – Ben May 07 '12 at 03:37
1

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

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89