本文翻译自:PHP parse/syntax errors; and how to solve them?
Everyone runs into syntax errors. 每个人都遇到语法错误。 Even experienced programmers make typos. 即使是经验丰富的程序员也会打错字。 For newcomers, it's just part of the learning process. 对于新手来说,这只是学习过程的一部分。 However, it's often easy to interpret error messages such as: 但是,通常很容易解释以下错误消息:
PHP Parse error: syntax error, unexpected '{' in index.php on line 20 PHP解析错误:语法错误,第20行的index.php中出现意外的'{'
The unexpected symbol isn't always the real culprit. 意外的符号并不总是真正的罪魁祸首。 But the line number gives a rough idea of where to start looking. 但是行号给出了从哪里开始寻找的粗略想法。
Always look at the code context . 始终查看代码上下文 。 The syntax mistake often hides in the mentioned or in previous code lines . 语法错误通常隐藏在提到的代码行 或 以前的代码行中 。 Compare your code against syntax examples from the manual. 将代码与手册中的语法示例进行比较。
While not every case matches the other. 虽然并非每种情况都匹配。 Yet there are some general steps to solve syntax mistakes . 但是,有一些常规步骤可以解决语法错误 。 This references summarized the common pitfalls: 这些参考文献总结了常见的陷阱:
Unexpected T_VARIABLE 意外的T_VARIABLE
Unexpected '$varname' (T_VARIABLE) 意外的'$ varname'(T_VARIABLE)Unexpected T_CONSTANT_ENCAPSED_STRING 意外的T_CONSTANT_ENCAPSED_STRING
Unexpected T_ENCAPSED_AND_WHITESPACE 意外的T_ENCAPSED_AND_WHITESPACEUnexpected
{
意外{
Unexpected}
意想不到的}
Unexpected(
意外的(
Unexpected)
意外)
Unexpected T_IF 意外的T_IF
Unexpected T_FOREACH 意外的T_FOREACH
Unexpected T_FOR 意外的T_FOR
Unexpected T_WHILE 意外的T_WHILE
Unexpected T_DO 意外的T_DO
Unexpected T_PRINT 意外的T_PRINT
Unexpected T_ECHO 意外的T_ECHOUnexpected continue (T_CONTINUE) 意外继续(T_CONTINUE)
Unexpected continue (T_BREAK) 意外继续(T_BREAK)
Unexpected continue (T_RETURN) 意外继续(T_RETURN)Unexpected T_PAAMAYIM_NEKUDOTAYIM … 意外的T_PAAMAYIM_NEKUDOTAYIM …
Unexpected T_BOOLEAN_OR … 意外的T_BOOLEAN_OR …
Unexpected T_BOOLEAN_AND … 意外的T_BOOLEAN_AND …Unexpected T_IS_EQUAL 意外的T_IS_EQUAL
Unexpected T_IS_GREATER_OR_EQUAL 意外的T_IS_GREATER_OR_EQUAL
Unexpected T_IS_IDENTICAL 意外的T_IS_IDENTICAL
Unexpected T_IS_NOT_EQUAL 意外的T_IS_NOT_EQUAL
Unexpected T_IS_NOT_IDENTICAL 意外的T_IS_NOT_IDENTICAL
Unexpected T_IS_SMALLER_OR_EQUAL 意外的T_IS_SMALLER_OR_EQUAL
Unexpected<
意外<
Unexpected>
意外>
Unexpected character in input: '
\\
' (ASCII=92) state=1 输入中出现意外字符:'\\
'(ASCII = 92)state = 1Unexpected 'public' (T_PUBLIC) 意外的“公开”(T_PUBLIC)
Unexpected 'private' (T_PRIVATE) 意外的“私人”(T_PRIVATE)
Unexpected 'protected' (T_PROTECTED) 意外的“受保护”(T_PROTECTED)
Unexpected 'final' (T_FINAL) … 意外的“最终”(T_FINAL) …Unexpected
,
(comma) 意外,
(逗号)Unpexected
.
不被感动.
(period) (期)Unexpected
;
出乎意料;
(semicolon) (分号)Unexpected
*
(asterisk) 意外的*
(星号)Unexpected
:
(colon) 意外:
冒号)
Closely related references: 密切相关的参考文献: