PHP EMPTY令牌

时间:2013-01-14 02:42:09

标签: php constants

define('EMPTY', '');
if(!empty(EMPTY))
{
if(!(boolean) _CONSTANT)
{
    print "One";
}
}

上面的代码会产生以下错误消息:

“第2行的C:\ Users \ Robert \ Documents \ web development \ xampp \ htdocs \ xampp \ web_development \ index.php中的解析错误:语法错误,意外'EMPTY'(T_EMPTY)”

PHP手册中没有任何地方明确声明“EMPTY”是一个令牌,它也没有告诉你这个陷阱。

3 个答案:

答案 0 :(得分:3)

您可以声明与函数同名的常量,因为PHP使用()来确定令牌是函数还是常量声明:

<?php
function myfunc() {
 echo "inside myfunc()\n";
}
define('str_replace', 'my string');
echo str_replace . "\n";
echo str_replace("A", "B", "AAAA") . "\n";
define('myfunc', 'this is myfunc constant');
echo myfunc . "\n";
myfunc();

/*
Output:
my string
BBBB
this is myfunc constant
inside myfunc()
 */

这对empty无效,因为emptylanguage construct

答案 1 :(得分:0)

这不是问题......只需为你的常数选择另一个名字。

如果您愿意,可以submit a documentation bug report

答案 2 :(得分:-1)

函数名称不区分大小写。

$state = constant('EMPTY');
if (!empty($state)) {