解析器记号(token)列表
PHP 语言的各个部分在内部使用记号表示。包含无效序列的代码片段可能会导致错误。例如 Parse error:
syntax error, unexpected token "==", expecting "(" in script.php on line
10."
。其中记号 ==
在内部由 T_IS_EQUAL
表示。
下表列出的所有记号。也可以用作 PHP 常量。
注意: T_* 常量用法
T_* 常量是根据 PHP 底层解析器数据结构自动生成的。这意味着记号的具体值可能会在不同的 PHP 版本之间发生变更。这也意味着代码不应直接从 PHP X.Y.Z 版本中获取原始 T_* 值,从而提供跨越多个版本的兼容性。
为了在多个 PHP 版本中使用 T_* 常量,用户可以对 PHP 版本和 T_* 值使用适当的策略(使用类似
10000
的大数),来定义未定义的常量。<?php
// 在 PHP 7.4.0 之前,未定义 T_FN。
defined('T_FN') || define('T_FN', 10001);
参见 token_name()。
+添加备注
用户贡献的备注 3 notes
daniel_rhodes at yahoo dot co dot uk ¶
10 months ago
In the above table of Tokens, it's worth noting that the bracketed text of "available as of PHP x.y.z" can mean one of two things:
[] This *parser token* is available as of PHP x.y.z
{eg. T_BAD_CHARACTER, T_NAME_QUALIFIED}
[] This *language feature* is available as of PHP x.y.z
{eg. T_ATTRIBUTE, T_COALESCE_EQUAL}
nathan at unfinitydesign dot com ¶
16 years ago
T_ENCAPSED_AND_WHITESPACE is whitespace which intersects a group of tokens. For example, an "unexpected T_ENCAPSED_AND_WHITESPACE" error is produced by the following code:
<?php
$main_output_world = 'snakes!';
echo('There are' 10 $main_output_world);
?>
Note the missing concatenation operator between the two strings leads to the whitespace error that is so named above. The concatenation operator instructs PHP to ignore the whitespace between the two code tokens (the so named "encapsed" data"), rather than parse it as a token itself.
The correct code would be:
<?php
$main_output_world = 'snakes!';
echo('There are' . 10 . $main_output_world);
?>
Note the addition of the concatenation operator between each token.
fgm at osinet dot fr ¶
16 years ago
T_ENCAPSED_AND_WHITESPACED is returned when parsing strings with evaluated content, like "some $value" or this example from the Strings reference page:
<?php
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
This last example is tokenized as:
T_ECHO
echo
T_WHITESPACE
%20 (a space character)
T_START_HEREDOC
<<
T_ENCAPSED_AND_WHITESPACE
My name is "
T_VARIABLE
$name
T_ENCAPSED_AND_WHITESPACE
". I am printing some
T_VARIABLE
$foo
T_OBJECT_OPERATOR
->
T_STRING
foo
T_ENCAPSED_AND_WHITESPACE
. Now, I am printing some
T_CURLY_OPEN
{
T_VARIABLE
$foo
T_OBJECT_OPERATOR
->
T_STRING
bar
(terminal)
[
T_LNUMBER
1
(terminal)
]
(terminal)
}
T_ENCAPSED_AND_WHITESPACE
. This should print a capital 'A': \x41
T_END_HEREDOC
EOT
(terminal)
;