Tokens that are strings and not arrays (including a T_* constant) can be misleading, ie "$a&$b" (bitwise operator) and "$a=&$b" (reference assignation) where both "&" are string tokens.
PHP 语言的不同部分在内部被表示为类似 T_SR 的类型。PHP 在解析错误时输出这样的标识符,例如 "Parse error: unexpected T_SR, expecting ',' or ';' in script.php on line 10."。
应该知道 T_SR 的含义。对于所有不知道的人,下表列出了这些标识符,PHP 语法和在手册中适当位置的参考。
代号 | 语法 | 参考 |
---|---|---|
T_ABSTRACT | abstract | 抽象类(自 PHP 5.0.0 起可用) |
T_AND_EQUAL | &= | 赋值运算符 |
T_ARRAY | array() | array(),数组语法 |
T_ARRAY_CAST | (array) | 类型转换 |
T_AS | as | foreach |
T_BAD_CHARACTER | 在 ASCII 32 之前除了 \t (0x09), \n (0x0a) 和 \r (0x0d) 以外的任何字符 | |
T_BOOLEAN_AND | && | 逻辑运算符 |
T_BOOLEAN_OR | || | 逻辑运算符 |
T_BOOL_CAST | (bool) 或 (boolean) | 类型转换 |
T_BREAK | break | break |
T_CASE | case | switch |
T_CATCH | catch | 异常处理(自 PHP 5.0.0 起可用) |
T_CHARACTER | ||
T_CLASS | class | 类和对象 |
T_CLONE | clone | 类和对象。仅用于 PHP 5。 |
T_CLOSE_TAG | ?> 或 %> | |
T_COMMENT | // 或 #,以及 PHP 5 下的 /* */ | 注释 |
T_CONCAT_EQUAL | .= | 赋值运算符 |
T_CONST | const | |
T_CONSTANT_ENCAPSED_STRING | "foo" 或 'bar' | 字符串语法 |
T_CONTINUE | continue | |
T_CURLY_OPEN | ||
T_DEC | -- | 递增/递减运算符 |
T_DECLARE | declare | declare |
T_DEFAULT | default | switch |
T_DIV_EQUAL | /= | 赋值运算符 |
T_DNUMBER | 0.12,等等 | 浮点数 |
T_DOC_COMMENT | /** */ | PHPDoc 风格的注释(仅用于 PHP 5) |
T_DO | do | do..while |
T_DOLLAR_OPEN_CURLY_BRACES | ${ | 复杂变量解析语法 |
T_DOUBLE_ARROW | => | 数组语法 |
T_DOUBLE_CAST | (real), (double) 或 (float) | 类型转换 |
T_DOUBLE_COLON | :: | 见下面的 T_PAAMAYIM_NEKUDOTAYIM |
T_ECHO | echo | echo |
T_ELSE | else | else |
T_ELSEIF | elseif | elseif |
T_EMPTY | empty | empty() |
T_ENCAPSED_AND_WHITESPACE | ||
T_ENDDECLARE | enddeclare | declare,替代语法 |
T_ENDFOR | endfor | for,替代语法 |
T_ENDFOREACH | endforeach | foreach,替代语法 |
T_ENDIF | endif | if,替代语法 |
T_ENDSWITCH | endswitch | switch,替代语法 |
T_ENDWHILE | endwhile | while,替代语法 |
T_END_HEREDOC | heredoc 语法 | |
T_EVAL | eval() | eval() |
T_EXIT | exit 或 die | exit(), die() |
T_EXTENDS | extends | extends,类和对象 |
T_FILE | __FILE__ | 常量 |
T_FINAL | final | Final 关键字(自 PHP 5.0.0 起可用) |
T_FOR | for | for |
T_FOREACH | foreach | foreach |
T_FUNCTION | function 或 cfunction | 函数 |
T_GLOBAL | global | 变量范围 |
T_HALT_COMPILER | __halt_compiler() | __halt_compiler(自 PHP 5.1.0 起可用) |
T_IF | if | if |
T_IMPLEMENTS | implements | 对象接口(自 PHP 5.0.0 起可用) |
T_INC | ++ | 递增/递减运算符 |
T_INCLUDE | include() | include |
T_INCLUDE_ONCE | include_once() | include_once |
T_INLINE_HTML | ||
T_INSTANCEOF | instanceof | type operators(仅 PHP5) |
T_INT_CAST | (int) 或 (integer) | 类型转换 |
T_INTERFACE | interface | 对象接口(自 PHP 5.0.0 起可用) |
T_ISSET | isset() | isset() |
T_IS_EQUAL | == | 比较运算符 |
T_IS_GREATER_OR_EQUAL | >= | 比较运算符 |
T_IS_IDENTICAL | === | 比较运算符 |
T_IS_NOT_EQUAL | != or <> | 比较运算符 |
T_IS_NOT_IDENTICAL | !== | 比较运算符 |
T_IS_SMALLER_OR_EQUAL | <= | 比较运算符 |
T_LINE | __LINE__ | 常量 |
T_LIST | list() | list() |
T_LNUMBER | 123,012,0x1ac,等等 | 整型 |
T_LOGICAL_AND | and | 逻辑运算符 |
T_LOGICAL_OR | or | 逻辑运算符 |
T_LOGICAL_XOR | xor | 逻辑运算符 |
T_MINUS_EQUAL | -= | 赋值运算符 |
T_ML_COMMENT | /* 和 */ | 注释(仅用于 PHP 4) |
T_MOD_EQUAL | %= | 赋值运算符 |
T_MUL_EQUAL | *= | 赋值运算符 |
T_NEW | new | 类和对象 |
T_NUM_STRING | ||
T_OBJECT_CAST | (object) | 类型转换 |
T_OBJECT_OPERATOR | -> | 类和对象 |
T_OPEN_TAG | <?php, <? 或 <% | 从 HTML 转义 |
T_OPEN_TAG_WITH_ECHO | <?= or <%= | 从 HTML 转义 |
T_OR_EQUAL | |= | 赋值运算符 |
T_PAAMAYIM_NEKUDOTAYIM | :: | ::。也定义为
T_DOUBLE_COLON 。 |
T_PLUS_EQUAL | += | 赋值运算符 |
T_PRINT | print() | |
T_PRIVATE | private | 类和对象。仅用于 PHP 5。 |
T_PUBLIC | public | 类和对象。仅用于 PHP 5。 |
T_PROTECTED | protected | 类和对象。仅用于 PHP 5。 |
T_REQUIRE | require() | require |
T_REQUIRE_ONCE | require_once() | require_once |
T_RETURN | return | 返回值 |
T_SL | << | 位运算符 |
T_SL_EQUAL | <<= | 赋值运算符 |
T_SR | >> | 位运算符 |
T_SR_EQUAL | >>= | 赋值运算符 |
T_START_HEREDOC | <<< | heredoc 语法 |
T_STATIC | static | 变量范围 |
T_STRING | ||
T_STRING_CAST | (string) | 类型转换 |
T_STRING_VARNAME | ||
T_SWITCH | switch | switch |
T_THROW | throw | 异常处理(自 PHP 5.0.0 起可用) |
T_TRY | try | 异常处理(自 PHP 5.0.0 起可用) |
T_UNSET | unset() | unset() |
T_UNSET_CAST | (unset) | (无文档; 类型为 NULL ) |
T_USE | use | (未实现) |
T_VAR | var | 类和对象 |
T_VARIABLE | $foo | 变量 |
T_WHILE | while | while,do..while |
T_WHITESPACE | ||
T_XOR_EQUAL | ^= | 赋值运算符 |
T_FUNC_C | __FUNCTION__ | 常量,自 PHP 4.3.0 起可用 |
T_CLASS_C | __CLASS__ | 常量,自 PHP 4.3.0 起可用 |
参见 token_name()。
Tokens that are strings and not arrays (including a T_* constant) can be misleading, ie "$a&$b" (bitwise operator) and "$a=&$b" (reference assignation) where both "&" are string tokens.
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)
;
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.