为何无法定义名为EMPTY的常量 [英] php - Why can't you define a constant named EMPTY
问题描述
所以...这更多是出于好奇,不是真正的问题。我不明白这一点:
<?php
define('EMPTY',1);
echo EMPTY;
结果为:解析错误:语法错误,意外的';',期望为'('
我首先想到的是保留空函数,但是后来我尝试了:
<?php
define('IS_ARRAY',1);
echo IS_ARRAY;
结果:1
所以……我不明白为什么为什么不能将空用作常量,例如is_array,等等。
empty
不是一个函数,但是语言结构(更像是运算符而不是函数),这意味着它是由解析器在解析时而不是在运行时解析的。它是一个保留关键字,您不能将其用于任何函数名或常量。 p>
empty
之所以不能成为常规函数的原因是因为其行为不同。 empty($ undefined )
首先会解析 $ undefined
,引发错误,然后将 null
传递给 empty
;确实应该防止 empty
的情况发生,因此它不能通过正常的函数调用规则进行播放。
So... This is more of a curiosity, not a real problem. I don't get this:
<?php
define('EMPTY', 1);
echo EMPTY;
Results in: Parse error: syntax error, unexpected ';', expecting '('
My first thought is that empty is reserved for the empty function. But then I tried:
<?php
define('IS_ARRAY', 1);
echo IS_ARRAY;
Result: 1
So... I don't get it. Why empty can't be used as constant while for example is_array, die, etc. can.
empty
is not a function but a language construct (more like an operator than a function), which means it's resolved by the parser at parse time and not by the runtime. It's a reserved keyword and you cannot use it for anything yourself like function names, or constants.
The reason empty
cannot be a regular function is because it behaves differently. empty($undefined)
would first resolve $undefined
, throw an error, then pass null
to empty
; which is exactly the case empty
is supposed to prevent, so it cannot play by the normal function invocation rules.
这篇关于为何无法定义名为EMPTY的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!