PHP是否记录了这种对退出和死亡的特殊处理? [英] Is this special treatment of exit and die documented in PHP?

查看:44
本文介绍了PHP是否记录了这种对退出和死亡的特殊处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了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

我发现特别感兴趣的 die exit ,因为尽管它们没有返回值,但它们仍可以用作PHP中的表达式.如下所示,所有代码行均引发语法错误:

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;

但是,以下PHP代码完全没有语法错误:

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;
}

我浏览了PHP文档,找不到关于die()和exit()的这种特殊处理的任何提及.是否有任何PHP专家知道这是否记录在任何地方.这是否是预期的行为,并且是否可以安全使用 isset($ _ GET ['bar'])或die(); 模式?会在将来的PHP版本中突然中断吗?

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?

推荐答案

die exit (它们共享 T_EXIT 令牌)属于 expr_without_variable 的规则解析阶段,这就是为什么PHP乐于将它们放在表达式上下文中而不会出现语法错误的原因.

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.

是否有任何PHP专家知道这是否记录在任何地方.

Do any PHP experts know if this is documented anywhere.

PHP手册中没有对特殊待遇的描述,但是 exit 手册页上的"nofollow>第一个示例显示它正被用作…或退出.

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.

这是否是预期的行为,并且是可以安全使用的 isset($ _ GET ['bar'])或die(); 模式;会在将来的PHP版本中突然中断吗?

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.

这篇关于PHP是否记录了这种对退出和死亡的特殊处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆