每个人都会遇到语法错误。即使是经验丰富的程序员也会出现拼写错误。对于新人来说,这只是学习过程的一部分。然而,通常很容易解释如下错误消息:
PHP解析错误:语法错误,在index.php第20行中出现意外的“{”
意想不到的符号并不总是真正的罪魁祸首。但是行号给出了从哪里开始查找的大致概念。
总是查看代码上下文。语法错误通常隐藏在前面提到的或前面的代码行中。将您的代码与手册中的语法示例进行比较。
但并不是所有情况都是一样的。但是有一些通用的步骤可以解决语法错误。
本文总结了常见的陷阱:
Unexpected T_STRING
Unexpected T_VARIABLE Unexpected '$varname' (T_VARIABLE)
Unexpected T_CONSTANT_ENCAPSED_STRING Unexpected T_ENCAPSED_AND_WHITESPACE
Unexpected $end
Unexpected T_FUNCTION…
Unexpected {Unexpected }Unexpected (Unexpected )
Unexpected [Unexpected ]
Unexpected T_IF Unexpected T_FOREACH Unexpected T_FOR Unexpected T_WHILE Unexpected T_DO Unexpected T_PRINT Unexpected T_ECHO
Unexpected T_LNUMBER
Unexpected ?
Unexpected continue (T_CONTINUE)Unexpected continue (T_BREAK)Unexpected continue (T_RETURN)
Unexpected '='
Unexpected T_INLINE_HTML…
Unexpected T_PAAMAYIM_NEKUDOTAYIM…
Unexpected T_OBJECT_OPERATOR…
Unexpected T_DOUBLE_ARROW…
Unexpected T_SL…
Unexpected T_BOOLEAN_OR…
Unexpected T_BOOLEAN_AND…
Unexpected T_IS_EQUAL
Unexpected T_IS_GREATER_OR_EQUAL
Unexpected T_IS_IDENTICAL
Unexpected T_IS_NOT_EQUAL
Unexpected T_IS_NOT_IDENTICAL
Unexpected T_IS_SMALLER_OR_EQUAL
Unexpected <
Unexpected >
Unexpected T_NS_SEPARATOR…
Unexpected character in input: '\' (ASCII=92) state=1
Unexpected 'public' (T_PUBLIC) Unexpected 'private' (T_PRIVATE) Unexpected 'protected' (T_PROTECTED) Unexpected 'final' (T_FINAL)…
Unexpected T_STATIC…
Unexpected T_CLASS…
Unexpected 'use' (T_USE)
Unexpected T_DNUMBER
Unexpected , (comma)
Unpexected . (period)
Unexpected ; (semicolon)
Unexpected * (asterisk)
Unexpected : (colon)
Unexpected ':', expecting ',' or ')'
Unexpected & (call-time pass-by-reference)
Unexpected .
密切相关的参考文献:
这个错误在PHP中意味着什么?(运行时错误)
解析错误:语法错误,意外的T_XXX
解析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE
解析错误:语法错误,意外的T_VARIABLE
这个符号在PHP中是什么意思?(语言标记)
这些“聪明”的引号对PHP毫无意义
And:
php.net上的PHP手册和它的各种语言标记
或者维基百科关于PHP的语法介绍。
最后是我们的php标签维基。
虽然Stack Overflow也欢迎新手程序员,但它主要针对的是专业编程问题。
回答每个人的编码错误和狭窄的拼写错误被认为是离题了。
因此,在发布语法修正请求之前,请花时间遵循基本步骤。
如果你仍然必须这样做,请展示你自己的解决方案,尝试修复,以及你对看起来或可能错误的思考过程。
如果您的浏览器显示错误消息,如“SyntaxError: illegal character”,那么它实际上不是php相关的,而是javascript语法错误。
供应商代码引起的语法错误:最后,考虑一下,如果语法错误不是由编辑代码库引起的,而是在外部供应商包安装或升级之后引起的,则可能是由于PHP版本不兼容造成的,因此请根据平台设置检查供应商的要求。
意想不到的美元结束
当PHP谈到“意外的$end”时,这意味着您的代码在解析器期望更多代码时结束了。(如果从字面上理解,这个信息有点误导。它不是关于一个名为“$end”的变量,就像新来者有时认为的那样。它指的是“文件的结束”,EOF。)
原因:代码块/和函数或类声明的{和}不平衡。
它几乎总是缺少一个}花括号来关闭前面的代码块。它的意思是,解析器希望找到一个结束},但实际上到达了文件的末尾。
同样,使用适当的缩进来避免此类问题。
使用带有括号匹配的IDE,找出}错误的地方。
在大多数ide和文本编辑器中都有快捷键:
NetBeans, PhpStorm, Komodo: Ctrl[和Ctrl]
Eclipse, Aptana: CtrlShiftP
Atom, Sublime: Ctrlm - Zend Studio Ctrlm
Geany, notepad++: CtrlB - Joe: CtrlG - Emacs: C-M-n - Vim: %
大多数ide还突出显示匹配的大括号、方括号和圆括号。
这使得检查它们的相关性变得非常容易:
无端接的表达式
对于未结束的表达式或语句,也会出现意外的$end语法/解析器错误:
$var = func(1,
?>EOF
因此,首先查看脚本的末尾。拖尾;在任何PHP脚本中,最后一条语句通常是多余的。但你应该有一个。正是因为它缩小了这些语法问题的范围。特别是在您发现自己在脚本末尾添加了更多语句之后。
缩进的HEREDOC标记
另一种常见情况出现在HEREDOC或NOWDOC字符串中。如果前面有空格、制表符等,终止标记将被忽略:
print <<< END
Content...
Content....
END;
# ↑ terminator isn't exactly at the line start
因此,解析器假定HEREDOC字符串将一直持续到文件的末尾(因此是“意外的$end”)。几乎所有的ide和语法高亮编辑器都会明确显示或发出警告。
转义的引号
如果你在字符串中使用\,它有一个特殊的含义。这称为“转义字符”,通常告诉解析器按字面意思取下一个字符。
示例:echo 'Jim said \'Hello\ ";将打印Jim说'hello'
如果转义字符串的结束引号,结束引号将被字面上理解,而不是像预期的那样,即作为字符串的一部分而不是结束字符串的可打印引号。这通常会在打开下一个字符串后或脚本结束时显示为解析错误。
在Windows中指定路径时非常常见的错误:“C:\xampp\htdocs\”是错误的。你需要“C:\\xampp\\htdocs\\ \”。另外,PHP通常会转换unix风格的路径(例如。“C:/xampp/htdocs/”)到Windows的正确路径。
替代语法
在模板中使用语句/代码块的替代语法时,很少会看到这种语法错误。使用if:和else:和一个缺失的endif;为例。
参见:
PHP语法错误“意外$end”
解析错误:语法错误,在我的PHP代码文件意外结束
解析错误语法错误文件意外结束,使用PHP
PHP解析错误:语法错误,CodeIgniter视图中的文件意外结束
解析错误:语法错误,文件意外结束(注册脚本)
“解析错误:语法错误,意外$end”为我的uni注册分配
修复PHP错误:PHP错误#3:文件意外结束
意想不到的T_IS_EQUAL
意想不到的T_IS_GREATER_OR_EQUAL
意想不到的T_IS_IDENTICAL
意想不到的T_IS_NOT_EQUAL
意想不到的T_IS_NOT_IDENTICAL
意想不到的T_IS_SMALLER_OR_EQUAL
意想不到的<
意想不到的>
比较运算符,如==,>=,===,!=,<>,!==和<=或<和>,主要应该只在表达式中使用,例如if表达式。如果解析器抱怨它们,那么通常意味着它们周围的paren不正确或不匹配()。
Parens grouping
In particular for if statements with multiple comparisons you must take care to correctly count opening and closing parenthesis:
⇓
if (($foo < 7) && $bar) > 5 || $baz < 9) { ... }
↑
Here the if condition here was already terminated by the )
Once your comparisons become sufficiently complex it often helps to split it up into multiple and nested if constructs rather.
isset() mashed with comparing
A common newcomer is pitfal is trying to combine isset() or empty() with comparisons:
⇓
if (empty($_POST["var"] == 1)) {
Or even:
⇓
if (isset($variable !== "value")) {
This doesn't make sense to PHP, because isset and empty are language constructs that only accept variable names. It doesn't make sense to compare the result either, because the output is only/already a boolean.
Confusing >= greater-or-equal with => array operator
Both operators look somewhat similar, so they sometimes get mixed up:
⇓
if ($var => 5) { ... }
You only need to remember that this comparison operator is called "greater than or equal" to get it right.
See also: If statement structure in PHP
Nothing to compare against
You also can't combine two comparisons if they pertain the same variable name:
⇓
if ($xyz > 5 and < 100)
PHP can't deduce that you meant to compare the initial variable again. Expressions are usually paired according to operator precedence, so by the time the < is seen, there'd be only a boolean result left from the original variable.
See also: unexpected T_IS_SMALLER_OR_EQUAL
Comparison chains
You can't compare against a variable with a row of operators:
⇓
$reult = (5 < $x < 10);
This has to be broken up into two comparisons, each against $x.
This is actually more a case of blacklisted expressions (due to equivalent operator associativity). It's syntactically valid in a few C-style languages, but PHP wouldn't interpret it as expected comparison chain either.
Unexpected > Unexpected <
The greater than > or less than < operators don't have a custom T_XXX tokenizer name. And while they can be misplaced like all they others, you more often see the parser complain about them for misquoted strings and mashed HTML:
⇓
print "<a href='z">Hello</a>";
↑
This amounts to a string "<a href='z" being compared > to a literal constant Hello and then another < comparison. Or that's at least how PHP sees it. The actual cause and syntax mistake was the premature string " termination.
It's also not possible to nest PHP start tags:
<?php echo <?php my_func(); ?>
↑
参见:
php T_IS_NOT_EQUAL错误
语法错误,意外的T_IS_EQUAL
返回语句的语法错误
http://forums.phpfreaks.com/topic/96891-parse-error-syntax-error-unexpected-t-is-not-identical-expecting-or/