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

99封情书 提交于 2021-02-04 13:32:05

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/10476028/is-this-special-treatment-of-exit-and-die-documented-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!