Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this special treatment of exit and die documented in PHP?

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:

  • empty
  • eval
  • include
  • include_once
  • isset
  • list
  • require
  • require_once
  • print

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

  • die
  • echo
  • exit
  • return
  • unset
  • __halt_compiler

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?

like image 425
Paul Avatar asked May 07 '12 02:05

Paul


2 Answers

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.

like image 193
Mahmoud Al-Qudsi Avatar answered Nov 11 '22 21:11

Mahmoud Al-Qudsi


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.

like image 24
salathe Avatar answered Nov 11 '22 22:11

salathe