PHP解析/语法错误;以及如何解决它们
每个人都遇到语法错误。即使是经验丰富的程序员也会错别字。对于新移民来说,这只是学习过程的一部分。但是,通常很容易解释错误消息,例如:
php解析错误:语法错误,在第20行上的index.php中出乎意料的'{'
意外符号并不总是真正的罪魁祸首。但是,线号对从哪里开始寻找大概的想法。
始终查看代码上下文。语法错误通常隐藏在以前的代码行中提到的或中。将您的代码与手册中的语法示例进行比较。
虽然并非每种情况都匹配对方。然而,有一些 解决语法错误的一般步骤 。 此引用总结了常见的陷阱:
- 意外的T_String
-
/A/18092288“>意外的T_Constant_encapsed_string
意外的T_ENCAPSED_AND_WHITESPACE -
- href =“ https://stackoverflow.com/questions/3723748/php-version-5-2-14-parse-error-syntax-error-eror-eror-ror--ror------------function-expect->出乎意料的T_FUNTICN …… /p>
- “ https://stackoverflow.com/a/18092318">未指示t_if
意外的T_foreach
意外的T_FOR
意外的T_DO
意外的T_DO
意外的T_PRINT
意外的T_ECHO
意外 -
-
-
-
nundended t_paamayim_nekudotayim ... ...
- p> 意外的t_object_operator …
- ” https://stackoverflow.com/questions/2622624/parse-error-syntax-error-erbected-t-double-arrow-php"> nuneped t_double_arrow_arrow ...
-
” https://stackoverflow.com/questions/11208725/parse-error-syntax-error-unceted-t-sl-php-heredoc">未指望的T_SL …
- “ https://stackoverflow.com/questions/4419095/syntax-error-error-uncreted-t-boolean-or"> undended t_boolean_or …
意外的t_boolean_and_and_and_and_and … - href =“ https://stackoverflow.com/a/30142092/345031”>出乎意料的t_is_equal
意外的T_is_greater_or_equal
意外的t_is_istinal
意外的T_IS_NOT_EQUAL
意外的t_is_not_istinal
意外的T_is_smaller_or_equal
意外<
意外>
-
://stackoverflow.com/questions/17156251/unexped-character-input-input-ascii-92-state-1“>输入中的意外字符:'
\
'(ascii = 92)state = state = 1 -
意外事件public'(t_public)
意外的“私有”(t_private)
意外的“受保护”(t_protected)
意外的'最终'(t_final)… -
-
出乎意料的
,
>(逗号) -
意外事件/code> (semicolon)
- ” https://stackoverflow.com/questions/33178974/parse-error-undardected-works-fine-in-localhost"> undended
:
(colon) - /Question/4665782/PHP-WARNING-CALL-PASS-BY“>出乎意料
紧密相关的引用:
和:
- php.net上的php手册及其各种语言tokens
- 或wikipedia php 的语法简介。
- 最后,我们的 php tag-wiki 当然。
尽管Stack Overflow也欢迎新秀编码人员,但它主要针对专业编程问题。
- 回答每个人的编码错误和狭窄的错别字。
- 因此,请花一些时间按照基本步骤,在发布语法修复请求之前。
- 如果您仍然需要,请展示您自己的解决计划,尝试修复程序以及有关外观或可能出错的思考过程。
如果您的浏览器显示错误消息,例如“ Syntaxerror:非法字符”,那么它实际上不是 php - 相关,但是a javascript - stytax错误。
在供应商代码上提出的语法错误:最后,考虑一下,如果不是通过编辑您的代码库来提出语法错误,但是在安装或升级外部供应商软件包后,可能是由于PHP版本不兼容,所以根据您的平台设置检查供应商的要求。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
语法错误是什么?
PHP属于 c-style 和命令编程语言。它具有严格的语法规则,在遇到放错位置符号或标识符时无法从中恢复。它无法猜测您的编码意图。
最重要的提示,
您可以始终采取一些基本的预防措施:
使用适当的代码凹痕,或采用任何崇高的编码样式。
可读性阻止了不规则性。
使用 iDe /a>带有语法突出显示。
这也有助于括号/支架平衡。
阅读语言参考和手册中的示例。
两次,要精通。
如何解释解析器错误
典型的语法错误消息读取:
列出了语法错误的可能的位置。请参阅提到的文件名和线号。
a noriker 例如
t_string
解释了哪个符号的解析器/tokenizer无法最终处理。但是,这不一定是语法错误的原因。还必须研究以前的代码行。通常,语法错误只是较早发生的不幸。错误行号只是解析器最终放弃处理所有问题的地方。
解决语法错误
有许多方法可以缩小和修复语法打ic。
打开提到的源文件。查看上述代码线。
对于失控的字符串和放错的操作员,通常是您找到罪魁祸首的地方。
读取从左到右的线,想象每个符号的作用。
更常规的是,您还需要查看上线。
特别是,缺少
;
在上一行的末端/语句中缺少半隆。 (至少从风格的角度来看。)如果
{
代码块}
是错误的关闭或嵌套,则可能需要进一步调查源代码。使用适当的代码凹痕来简化。查看语法着色!
字符串,变量和常数都应具有不同的颜色。
运算符
+ - */。
也应被不同。否则它们可能处于错误的上下文。如果您看到字符串着色延伸太远或太短,则您发现了一个毫无疑问的或缺少关闭
“
”或''字符串标记。彼此之间有两个相同的标点符号也可能意味着麻烦。通常,如果不是
++
, - 或操作员之后的括号,则操作员是唯一的。在大多数情况下,两个字符串/标识符彼此互相关注。Whitespace是您的朋友。
遵循任何编码样式。
暂时分解长线。
您可以在操作员或常数和字符串之间自由 。然后,解析器将对解析错误的行号进行凝结。您可以隔离缺失或放错的语法符号,而不是查看非常冗长的代码。
如果语句分为 条件。
而不是冗长的数学公式或逻辑链,而是使用临时变量来简化代码。 (更可读=更少的错误。)
在:
之间添加新线
划分长代码块真的有助于找到语法错误的起源。
评论有问题的代码。
如果您无法隔离问题源,请开始评论(并因此暂时删除)代码块。
一旦摆脱解析错误,就找到了问题来源。在那里更近距离看。
有时您想暂时删除完整的功能/方法块。 (如果有无与伦比的卷发括号和错误的缩进代码。)
当您无法解决语法问题时,尝试重写从头开始注释的部分。
作为新来者,避免一些令人困惑的语法构造。
三元代码>? :条件操作员可以紧凑代码,并且确实很有用。但是在所有情况下,它都不有助于可读性。如果语句未经证实。
,请使用普通
。
php的替代语法(
blocks。if:
/elseif:
/endif;
)对于模板而言是常见的,但可以说较容易容易遵循比正常{
代码<代码>}最普遍的新人错误是:
缺少半洛龙
;
用于终止语句/行。的不匹配的字符串引号“
或'
和unscaped引号。忘记的操作员,特别是字符串
。
convenation。不平衡
(
括号)
。在报告的行中计算它们。有相等数量吗?不要忘记解决一个语法问题可能会发现下一个问题。
如果您使一个问题消失了,但是在下面的某些代码中进行其他农作物,则您主要是正确的路径。
如果编辑新的语法错误在同一行中播种后,则您的尝试更改可能是故障。 (并非总是如此。)
如果无法修复,请还原以前工作代码的备份。
diff
。关于语法问题是什么,这可能是启发性的。看不见的杂散unicode字符:在某些情况下,您需要在您的来源上使用六边形或其他编辑器/查看器。仅通过查看您的代码就找不到一些问题。
尝试
grep -color -p -n“ \ [\ x80- \ xff \]” file.php
作为查找非ASCII符号的第一个措施。尤其是boms,零宽的空间或非破裂的空间,并且智能报价定期可以找到源代码。
注意哪种类型的线折扣保存在文件中。
php Just honors \ n newlines,而不是 \ r 马车返回。
这有时是MacOS用户的问题(即使在OS&nbsp; x上也适用于错误配置的编辑器)。
单行
//
或#
注释时,它通常仅作为问题浮出水面。 Multiline//......./
评论很少在忽略线路破裂时打扰解析器。如果您的语法错误不会通过网络传输:
碰巧的是,您的计算机上有语法错误。但是在线发布同一文件不再显示出来。这只能意味着两件事之一:
您正在查看错误的文件!
或您的代码包含不可见的杂物Unicode(请参见上文)。
您可以轻松发现:只需将代码从Web表单复制回文本编辑器。
检查您的 PHP版本。并非所有语法构造都可以在每个服务器上提供。
php -v
用于命令行解释器&lt;?php phpinfo();
是通过Web服务器调用的。这些不一定是相同的。特别是在使用框架时,您将它们匹配。
不要使用 php的保留关键字作为函数/方法,类或常数的标识符。
反复试验是您的最后一个度假胜地。
如果所有其他方法都失败了,则可以始终 Google 您的错误消息。语法符号并不那么容易搜索(堆栈溢出本身由 symborhound )。因此,在找到相关的内容之前,可能需要浏览几页。
进一步指南:
白屏幕死亡屏幕
,如果您的网站只是空白,则通常是原因。
使用以下方式启用其显示:
error_reporting = e_all
display_errors = 1
在您的
php.ini
通常,或通过.htaccess
对于mod_php,甚至
.user.ini
使用FastCGI设置。在损坏脚本中启用它为时已晚,因为PHP甚至无法解释/运行第一行。快速的解决方法正在制作包装脚本,例如
test.php
:然后通过访问此包装脚本来调用失败代码。
它还有助于启用PHP的
error_log
,并查看您的 webserver的error.log
当脚本用HTTP 500响应崩溃时。
What are the syntax errors?
PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.
Most important tips
There are a few basic precautions you can always take:
Use proper code indentation, or adopt any lofty coding style.
Readability prevents irregularities.
Use an IDE or editor for PHP with syntax highlighting.
Which also help with parentheses/bracket balancing.
Read the language reference and examples in the manual.
Twice, to become somewhat proficient.
How to interpret parser errors
A typical syntax error message reads:
Which lists the possible location of a syntax mistake. See the mentioned file name and line number.
A moniker such as
T_STRING
explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.
Solving syntax errors
There are many approaches to narrow down and fix syntax hiccups.
Open the mentioned source file. Look at the mentioned code line.
For runaway strings and misplaced operators, this is usually where you find the culprit.
Read the line left to right and imagine what each symbol does.
More regularly you need to look at preceding lines as well.
In particular, missing
;
semicolons are missing at the previous line ends/statement. (At least from the stylistic viewpoint. )If
{
code blocks}
are incorrectly closed or nested, you may need to investigate even further up the source code. Use proper code indentation to simplify that.Look at the syntax colorization!
Strings and variables and constants should all have different colors.
Operators
+-*/.
should be tinted distinct as well. Else they might be in the wrong context.If you see string colorization extend too far or too short, then you have found an unescaped or missing closing
"
or'
string marker.Having two same-colored punctuation characters next to each other can also mean trouble. Usually, operators are lone if it's not
++
,--
, or parentheses following an operator. Two strings/identifiers directly following each other are incorrect in most contexts.Whitespace is your friend.
Follow any coding style.
Break up long lines temporarily.
You can freely add newlines between operators or constants and strings. The parser will then concretize the line number for parsing errors. Instead of looking at the very lengthy code, you can isolate the missing or misplaced syntax symbol.
Split up complex
if
statements into distinct or nestedif
conditions.Instead of lengthy math formulas or logic chains, use temporary variables to simplify the code. (More readable = fewer errors.)
Add newlines between:
Partitioning up long code blocks really helps to locate the origin of syntax errors.
Comment out offending code.
If you can't isolate the problem source, start to comment out (and thus temporarily remove) blocks of code.
As soon as you got rid of the parsing error, you have found the problem source. Look more closely there.
Sometimes you want to temporarily remove complete function/method blocks. (In case of unmatched curly braces and wrongly indented code.)
When you can't resolve the syntax issue, try to rewrite the commented out sections from scratch.
As a newcomer, avoid some of the confusing syntax constructs.
The ternary
? :
condition operator can compact code and is useful indeed. But it doesn't aid readability in all cases. Prefer plainif
statements while unversed.PHP's alternative syntax (
if:
/elseif:
/endif;
) is common for templates, but arguably less easy to follow than normal{
code}
blocks.The most prevalent newcomer mistakes are:
Missing semicolons
;
for terminating statements/lines.Mismatched string quotes for
"
or'
and unescaped quotes within.Forgotten operators, in particular for the string
.
concatenation.Unbalanced
(
parentheses)
. Count them in the reported line. Are there an equal number of them?Don't forget that solving one syntax problem can uncover the next.
If you make one issue go away, but other crops up in some code below, you're mostly on the right path.
If after editing a new syntax error crops up in the same line, then your attempted change was possibly a failure. (Not always though.)
Restore a backup of previously working code, if you can't fix it.
diff
of the broken and last working version. Which might be enlightening as to what the syntax problem is.Invisible stray Unicode characters: In some cases, you need to use a hexeditor or different editor/viewer on your source. Some problems cannot be found just from looking at your code.
Try
grep --color -P -n "\[\x80-\xFF\]" file.php
as the first measure to find non-ASCII symbols.In particular BOMs, zero-width spaces, or non-breaking spaces, and smart quotes regularly can find their way into the source code.
Take care of which type of linebreaks are saved in files.
PHP just honors \n newlines, not \r carriage returns.
Which is occasionally an issue for MacOS users (even on OS X for misconfigured editors).
It often only surfaces as an issue when single-line
//
or#
comments are used. Multiline/*...*/
comments do seldom disturb the parser when linebreaks get ignored.If your syntax error does not transmit over the web:
It happens that you have a syntax error on your machine. But posting the very same file online does not exhibit it anymore. Which can only mean one of two things:
You are looking at the wrong file!
Or your code contained invisible stray Unicode (see above).
You can easily find out: Just copy your code back from the web form into your text editor.
Check your PHP version. Not all syntax constructs are available on every server.
php -v
for the command line interpreter<?php phpinfo();
for the one invoked through the webserver.Those aren't necessarily the same. In particular when working with frameworks, you will them to match up.
Don't use PHP's reserved keywords as identifiers for functions/methods, classes or constants.
Trial-and-error is your last resort.
If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.
Further guides:
White screen of death
If your website is just blank, then typically a syntax error is the cause.
Enable their display with:
error_reporting = E_ALL
display_errors = 1
In your
php.ini
generally, or via.htaccess
for mod_php,or even
.user.ini
with FastCGI setups.Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say
test.php
:Then invoke the failing code by accessing this wrapper script.
It also helps to enable PHP's
error_log
and look into your webserver'serror.log
when a script crashes with HTTP 500 responses.我认为这个话题完全被过度讨论/过度复杂化。使用IDE是完全避免任何语法错误的方法。我什至会说,没有IDE的工作是不专业的。为什么?因为现代IDE在您输入的每个字符后检查您的语法。当您编码和整个行变红色时,一个很大的警告通知显示了语法错误的确切类型和确切位置,则绝对无需搜索其他解决方案。
使用语法检查IDE的意思是:
您(有效)您再也不会陷入语法错误,仅仅是因为您在键入时会看到它们正确。 。
地
I think this topic is totally overdiscussed/overcomplicated. Using an IDE is THE way to go to completely avoid any syntax errors. I would even say that working without an IDE is kind of unprofessional. Why? Because modern IDEs check your syntax after every character you type. When you code and your entire line turns red, and a big warning notice shows you the exact type and the exact position of the syntax error, then there's absolutely no need to search for another solution.
Using a syntax-checking IDE means:
You'll (effectively) never run into syntax errors again, simply because you see them right as you type. Seriously.
Excellent IDEs with syntax check (all of them are available for Linux, Windows and Mac):
出乎意料的
[
这些天,意外的
[
数组括号通常在过时的PHP版本上看到。 短阵列语法是因为PHP = 5.4 = 5.4 。较旧的安装仅支持array()
。阵列功能结果同样不适合较旧的PHP版本:
参考 - 此错误在PHP中意味着什么? - “语法错误,出乎意料的
\ [
'显示了最常见和最实用的解决方法。不过,只需升级PHP安装即可,您总是最好。对于共享的Web托管计划,首先研究如果EG
Sethandler PHP56-FCGI
可以用于启用更新的运行时。另请参阅:
btw,也有预处理程序, php 5.4语法下传感器如果您真的很贴上较旧 +较慢的php版本。
意外的其他原因
[
语法错误如果不是PHP版本不匹配,则通常是一个普通的错别字或新的语法错误:
您不能使用,甚至在PHP&nbsp; 7。
中也没有
混淆
[
与打开卷曲括号{
或括号(
是一个常见的疏忽。甚至:
至少php将
const
解释为常数名称。如果您打算访问数组变量(这是此处的典型原因),则添加领先的
$
sigil-因此,它将变成$ varname
。 p>您正在尝试在关联数组成员上使用
全局
关键字。这不是有效的语法:意外
]
关闭 square bracket这有些稀有,但是终端数组
支架。
再次与
)
括号或}
卷曲支架很常见:或试图结束一个没有一个的数组:
通常发生在多线和 nested 数组声明中。
如果是这样,请使用您的IDE进行括号匹配以找到任何过早的
]
数组闭合。至少使用更多的间距和新线来缩小范围。Unexpected
[
These days, the unexpected
[
array bracket is commonly seen on outdated PHP versions. The short array syntax is available since PHP >= 5.4. Older installations only supportarray()
.Array function result dereferencing is likewise not available for older PHP versions:
Reference - What does this error mean in PHP? - "Syntax error, unexpected
\[
" shows the most common and practical workarounds.Though, you're always better off just upgrading your PHP installation. For shared webhosting plans, first research if e.g.
SetHandler php56-fcgi
can be used to enable a newer runtime.See also:
BTW, there are also preprocessors and PHP 5.4 syntax down-converters if you're really clingy with older + slower PHP versions.
Other causes for Unexpected
[
syntax errorsIf it's not the PHP version mismatch, then it's oftentimes a plain typo or newcomer syntax mistake:
You can't use array property declarations/expressions in classes, not even in PHP 7.
Confusing
[
with opening curly braces{
or parentheses(
is a common oversight.Or even:
Or trying to dereference constants (before PHP 5.6) as arrays:
At least PHP interprets that
const
as a constant name.If you meant to access an array variable (which is the typical cause here), then add the leading
$
sigil - so it becomes a$varname
.You are trying to use the
global
keyword on a member of an associative array. This is not valid syntax:Unexpected
]
closing square bracketThis is somewhat rarer, but there are also syntax accidents with the terminating array
]
bracket.Again mismatches with
)
parentheses or}
curly braces are common:Or trying to end an array where there isn't one:
Which often occurs in multi-line and nested array declarations.
If so, use your IDE for bracket matching to find any premature
]
array closure. At the very least use more spacing and newlines to narrow it down.意外的t_variable
“意外
t_variable
”表示有一个文字$ variable
名称,该名称不适合当前表达式/语句结构。缺少semicolon
最常见的是缺少的半olon 在上一行中。语句之后的可变分配是一个很好的指示:
字符串串联
频繁的不良事件是忘记
。
操作员:顺便说一句,您应该喜欢 string interpolation (基本变量引用)每当有助于可读性时。避免了这些语法问题。
缺少表达式操作员
当然,其他表达式可能会出现同样的问题,例如算术操作:
php不能猜测如果应该添加,减法或比较该变量等。
lists < /H3>
对于语法列表而言,例如在数组群体中,解析器还指示了预期的逗号
,
例如:或功能参数列表:
等效地,您是否在
list
或global
语句中看到了这一点? >循环。类声明
此解析器错误也发生在类声明中。您只能分配静态常数,而不是表达式。因此,解析器抱怨变量作为分配的数据:
无与伦比的
}
尤其可以在这里引导卷曲括号。如果方法终止得太早(使用适当的凹痕!),则通常将一个杂散变量放在类声明主体中。标识符后变量
您也永远无法拥有一个可变的drialible of clocter vishible 直接直接: /p>
btw,这是一个常见的示例,其中意图是使用变量变量也许。在这种情况下
语言构造后缺少括号
急速打字可能会导致遗忘的开放或关闭括号
对于 和
for
和foreach
语句:解决方案:在语句和变量之间添加丢失的打开(。
curly
{
brace不会打开代码块,而不会关闭使用使用>)
首先关闭括号。其他不期望条件
解决方案:从
else
中删除条件或使用elseif
。需要封闭括号
解决方案:添加括号
$ var
。无形的whitespace
如参考答案在“ Invisible Stray Unicode”上//en.wikipedia.org/wiki/non-breaking_space“ rel =“ noreferrer”>非破坏空间),您可能还会看到此错误的毫无戒心的代码,例如:
在文件的开始和复制和贴上代码中相当普遍。请访问六角形,如果您的代码在视觉上似乎包含语法问题。
另请参见
Unexpected T_VARIABLE
An "unexpected
T_VARIABLE
" means that there's a literal$variable
name, which doesn't fit into the current expression/statement structure.Missing semicolon
It most commonly indicates a missing semicolon in the previous line. Variable assignments following a statement are a good indicator where to look:
String concatenation
A frequent mishap are string concatenations with forgotten
.
operator:Btw, you should prefer string interpolation (basic variables in double quotes) whenever that helps readability. Which avoids these syntax issues.
Missing expression operators
Of course the same issue can arise in other expressions, for instance arithmetic operations:
PHP can't guess here if the variable should have been added, subtracted or compared etc.
Lists
Same for syntax lists, like in array populations, where the parser also indicates an expected comma
,
for example:Or functions parameter lists:
Equivalently do you see this with
list
orglobal
statements, or when lacking a;
semicolon in afor
loop.Class declarations
This parser error also occurs in class declarations. You can only assign static constants, not expressions. Thus the parser complains about variables as assigned data:
Unmatched
}
closing curly braces can in particular lead here. If a method is terminated too early (use proper indentation!), then a stray variable is commonly misplaced into the class declaration body.Variables after identifiers
You can also never have a variable follow an identifier directly:
Btw, this is a common example where the intention was to use variable variables perhaps. In this case a variable property lookup with
$this->{"myFunc$VAR"}();
for example.Missing parentheses after language constructs
Hasty typing may lead to forgotten opening or closing parenthesis
for
if
andfor
andforeach
statements:Solution: add the missing opening
(
between statement and variable.The curly
{
brace does not open the code block, without closing theif
expression with the)
closing parenthesis first.Else does not expect conditions
Solution: Remove the conditions from
else
or useelseif
.Need brackets for closure
Solution: Add brackets around
$var
.Invisible whitespace
As mentioned in the reference answer on "Invisible stray Unicode" (such as a non-breaking space), you might also see this error for unsuspecting code like:
It's rather prevalent in the start of files and for copy-and-pasted code. Check with a hexeditor, if your code does not visually appear to contain a syntax issue.
See also
意外的t_constant_encapsed_string
意外的t_encapsed_and_whitespace
undieldy名称
t_constant_encapsed_string_string
和t_encapsed_and_whitespace
请参阅引号文字。它们在不同的上下文中使用,但是语法问题非常相似。 t_encapsed…警告出现在双引号的字符串上下文中,而 t_constant…字符串通常误入歧途PHP表达式或语句。
不正确的变量插值
,对于不正确的PHP变量插值,它最常出现:
引用数组键是PHP上下文中必须的。但是在双引号(或Heredocs)中,这是一个错误。解析器抱怨包含的单个引号
'string'
,因为它通常期望在那里有字面的标识符/键。更确切地说,使用php2-style 数组参考:
嵌套阵列或更深的对象引用,但是需要复杂的卷曲字符串表达式语法:
如果不确定,通常使用更安全。它通常被认为更可读。更好的IDE实际上使用了不同的语法着色。
缺少串联
如果字符串遵循表达式,但缺乏串联或其他操作员,那么您会看到PHP抱怨字符串字面:
虽然对您和我很明显,但PHP不能猜测 string string是在那里附加的。
混淆字符串报价外壳
混淆字符串定界器时,发生了相同的语法错误。 。一个字符串由单个
'
或double开始,
引用也以相同的结尾。该示例以双引号开头。但是双引号也注定是针对HTML属性的。然而,内部的预期串联操作员被解释为单引号中的第二个字符串的一部分。
这是一个很好的例子,您首先不应该突破双引号。相反,只需使用逃脱 html属性的报价:
虽然这也会导致语法混乱,但所有更好的IDE/编辑者都会以不同的方式着色。
等效地为忘记的打开
/code>/
'
引用解析器错误的食谱:
在这里,
','
将在裸词之后成为字符串字面,而显然登录
是字符串参数。如果您错过了
,
逗号在数组创建块中,解析器将看到两个连续的字符串:请注意,最后一行可能始终包含一个额外的逗号,但是在介于两者之间的一行是不可原谅的。如果没有语法突出显示,这很难发现。
功能参数列表
同一件事用于函数调用:
一个常见的变化是简单地忘记的字符串终结者:
这里的PHP抱怨两个字符串文字直接彼此关注。但是真正的原因当然是未锁定的先前字符串。
Heredoc凹痕
先验 php 7.3 ,
解决方案:升级PHP或找到更好的寄养者。
另请参见
Unexpected T_CONSTANT_ENCAPSED_STRING
Unexpected T_ENCAPSED_AND_WHITESPACE
The unwieldy names
T_CONSTANT_ENCAPSED_STRING
andT_ENCAPSED_AND_WHITESPACE
refer to quoted"string"
literals.They're used in different contexts, but the syntax issue are quite similar. T_ENCAPSED… warnings occur in double quoted string context, while T_CONSTANT… strings are often astray in plain PHP expressions or statements.
Incorrect variable interpolation
And it comes up most frequently for incorrect PHP variable interpolation:
Quoting arrays keys is a must in PHP context. But in double quoted strings (or HEREDOCs) this is a mistake. The parser complains about the contained single quoted
'string'
, because it usually expects a literal identifier / key there.More precisely it's valid to use PHP2-style simple syntax within double quotes for array references:
Nested arrays or deeper object references however require the complex curly string expression syntax:
If unsure, this is commonly safer to use. It's often even considered more readable. And better IDEs actually use distinct syntax colorization for that.
Missing concatenation
If a string follows an expression, but lacks a concatenation or other operator, then you'll see PHP complain about the string literal:
While it's obvious to you and me, PHP just can't guess that the string was meant to be appended there.
Confusing string quote enclosures
The same syntax error occurs when confounding string delimiters. A string started by a single
'
or double"
quote also ends with the same.That example started with double quotes. But double quotes were also destined for the HTML attributes. The intended concatenation operator within however became interpreted as part of a second string in single quotes.
This is a good example where you shouldn't break out of double quotes in the first place. Instead just use proper
\"
escapes for the HTML attributes´ quotes:While this can also lead to syntax confusion, all better IDEs/editors again help by colorizing the escaped quotes differently.
Missing opening quote
Equivalently are forgotten opening
"
/'
quotes a recipe for parser errors:Here the
', '
would become a string literal after a bareword, when obviouslylogin
was meant to be a string parameter.Array lists
If you miss a
,
comma in an array creation block, the parser will see two consecutive strings:Note that the last line may always contain an extra comma, but overlooking one in between is unforgivable. Which is hard to discover without syntax highlighting.
Function parameter lists
The same thing for function calls:
Runaway strings
A common variation are quite simply forgotten string terminators:
Here PHP complains about two string literals directly following each other. But the real cause is the unclosed previous string of course.
HEREDOC indentation
Prior PHP 7.3, the heredoc string end delimiter can't be prefixed with spaces:
Solution: upgrade PHP or find a better hoster.
See also
意外的T_STRING
T_STRING
有点不正确。它没有引用“字符串”
。这意味着遇到了原始标识符。从裸露的
单词到剩余的常数
或功能名称,被遗忘的单字符串或任何纯文本。错误引用的字符串
此语法错误对于错误引用的字符串值最常见。任何UneScaped and Stray
“
或'
报价都将形成无效的表达式:语法突出显示将使这样的错误变得非常明显。重要的是要记住使用backSlashes逃脱
\“
double Qualtes或\'
单QUALES-取决于用作“
double quotes。echo
/打印
行,而不是逃脱进出。最好考虑A heredoc pection另一个示例是使用PHP生成的HTML代码内的PHP条目:
如果
$ text
有很多线路,并且开发人员没有看到整个PHP变量值,并且专注于忘记其源的代码片段,则会发生这种情况。示例为在这里另请参见 PHP中的单引号和双引号的字符串有什么区别? 。
如果您 然后,语法错误通常以后进行。
当时解析器可能会抗议,这不仅是文字 t_string 。另一个频繁的变化是
意外'&gt;'
未引用的文字html。
如果您复制和粘贴代码从博客或网站上,有时您最终会得到无效的代码。 印刷报价不是 php期望的是什么:
印刷/智能引号是Unicode符号。 PHP将它们视为毗邻字母数字文本的一部分。例如,
“这些
被解释为常数标识符。但是,解析器将以下任何文字视为裸词/t_string。缺失的半隆;再次
如果您在以前的行中有未终止的表达式,则以下任何语句或语言构造被视为原始标识符:
php只是不知道您是否打算在另一个接一个地运行两个功能,或者是否要乘以它们的结果,添加它们,比较它们,或者仅运行一个
||
或另一个。短打开标签和
&lt;?xml
php脚本中的标题这很少见。但是,如果启用了short_open_tags,则无法开始php脚本:
php将看到
&lt;?
并为其本身收回。它不会理解杂散xml
的目的。它将被解释为恒定。但是版本
将被视为另一个文字/常数。而且,由于没有表达式操作员之间的两个随后的文字/值无法理解,这将是解析器的失败。隐形Unicode字符
语法错误的最丑陋原因是Unicode符号,例如。 PHP允许Unicode字符作为标识符名称。如果您收到T_String解析器的完全不疑问的代码,例如:
您需要打破另一个文本编辑器。甚至是六角形。这里看起来像普通的空间和新线,可能包含无形的常数。有时基于Java的IDE有时会忽略在内部固定的UTF-8 BOM,零宽的空间,段落分离器等。
您可以在每行添加冗余
;
语句分隔符中缩小范围:额外的
;
Semicolon在这里将将前面的不可见字符转换为未定义的常数参考(表达式为语句)。作为回报,PHP产生了有用的通知。``$'''符号在变量名称前缺少
php中的变量变量的名称。
美元符号(
$
)是将标识符标记为变量的名称。如果没有此sigil,标识符可以为a 语言关键字或a 常数。当PHP代码为“翻译为用另一种语言的代码翻译”时,这是一个常见错误。 ,JavaScript等)。在这种情况下,变量类型的声明(当原始代码用使用键入变量的语言编写时)也可能会偷偷摸摸并产生此错误。
逃脱的引号
如果您在字符串中使用
\
,则具有特殊的含义。这称为“ 逃脱字符“从字面上看下一个角色。示例:
回声'
如果您逃脱了字符串的关闭报价,则将关闭的报价从字面上拿走,而不是按预期进行,即作为字符串的一部分而不是关闭字符串的可打印报价。打开下一个字符串或在脚本末尾,通常将其显示为解析错误。
。
在Windows中指定路径时非常常见的错误:
“ C:\ XAMPP \ htdocs \”
是错误的。您需要“ c:\\ xampp \\ htdocs \\”
。键入属性
您需要php≥7.4才能使用属性property property typing
Unexpected T_STRING
T_STRING
is a bit of a misnomer. It does not refer to a quoted"string"
. It means a raw identifier was encountered. This can range frombare
words to leftoverCONSTANT
or function names, forgotten unquoted strings, or any plain text.Misquoted strings
This syntax error is most common for misquoted string values however. Any unescaped and stray
"
or'
quote will form an invalid expression:Syntax highlighting will make such mistakes super obvious. It's important to remember to use backslashes for escaping
\"
double quotes, or\'
single quotes - depending on which was used as string enclosure."
double quotes.echo
/print
lines instead of escaping in and out. Better yet consider a HEREDOC section.Another example is using PHP entry inside HTML code generated with PHP:
This happens if
$text
is large with many lines and developer does not see the whole PHP variable value and focus on the piece of code forgetting about its source. Example is hereSee also What is the difference between single-quoted and double-quoted strings in PHP?.
Unclosed strings
If you miss a closing
"
then a syntax error typically materializes later. An unterminated string will often consume a bit of code until the next intended string value:It's not just literal
T_STRING
s which the parser may protest then. Another frequent variation is anUnexpected '>'
for unquoted literal HTML.Non-programming string quotes
If you copy and paste code from a blog or website, you sometimes end up with invalid code. Typographic quotes aren't what PHP expects:
Typographic/smart quotes are Unicode symbols. PHP treats them as part of adjoining alphanumeric text. For example
”these
is interpreted as a constant identifier. But any following text literal is then seen as a bareword/T_STRING by the parser.The missing semicolon; again
If you have an unterminated expression in previous lines, then any following statement or language construct gets seen as raw identifier:
PHP just can't know if you meant to run two functions after another, or if you meant to multiply their results, add them, compare them, or only run one
||
or the other.Short open tags and
<?xml
headers in PHP scriptsThis is rather uncommon. But if short_open_tags are enabled, then you can't begin your PHP scripts with an XML declaration:
PHP will see the
<?
and reclaim it for itself. It won't understand what the strayxml
was meant for. It'll get interpreted as constant. But theversion
will be seen as another literal/constant. And since the parser can't make sense of two subsequent literals/values without an expression operator in between, that'll be a parser failure.Invisible Unicode characters
A most hideous cause for syntax errors are Unicode symbols, such as the non-breaking space. PHP allows Unicode characters as identifier names. If you get a T_STRING parser complaint for wholly unsuspicious code like:
You need to break out another text editor. Or an hexeditor even. What looks like plain spaces and newlines here, may contain invisible constants. Java-based IDEs are sometimes oblivious to an UTF-8 BOM mangled within, zero-width spaces, paragraph separators, etc. Try to reedit everything, remove whitespace and add normal spaces back in.
You can narrow it down with with adding redundant
;
statement separators at each line start:The extra
;
semicolon here will convert the preceding invisible character into an undefined constant reference (expression as statement). Which in return makes PHP produce a helpful notice.The `$` sign missing in front of variable names
Variables in PHP are represented by a dollar sign followed by the name of the variable.
The dollar sign (
$
) is a sigil that marks the identifier as a name of a variable. Without this sigil, the identifier could be a language keyword or a constant.This is a common error when the PHP code was "translated" from code written in another language (C, Java, JavaScript, etc.). In such cases, a declaration of the variable type (when the original code was written in a language that uses typed variables) could also sneak out and produce this error.
Escaped Quotation marks
If you use
\
in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.Example:
echo 'Jim said \'Hello\'';
will printJim said 'hello'
If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.
Very common error when specifiying paths in Windows:
"C:\xampp\htdocs\"
is wrong. You need"C:\\xampp\\htdocs\\"
.Typed properties
You need PHP ≥7.4 to use property typing such as:
意外
(
打开括号通常遵循语言构造,例如/
foreach
/for
/for
/array
代码>/<代码>列表或启动算术表达式。 上下文中。此错误的罕见发生是试图将表达式用作默认功能参数。即使在php7中也不支持这一点:
函数声明中的参数只能是文字值或常数表达式。与功能调用不同,您可以在其中自由使用
whything(1+theings()*2)
等。类成员声明允许文字/常数值,而不是表达式:
将这样的东西放入构造函数中。
另请参阅为什么PHP属性不允许函数?
再次注意,PHP 7仅允许
var $ xy = 1 + 2 +3;
在那里的常数表达式。php
中的JavaScript语法
使用javascript或 jquery Syntax 出于明显的原因,在PHP中:
发生这种情况时,通常表示前面的未终止的字符串;和文字
&lt; script&gt;
插件泄漏到php代码上下文isset(()),空,键,下一个,当前
isset()
和empty()
是语言内置,而不是函数。他们 /a>。如果您无意中添加了一对括号,则可以创建一个表达式:同样适用于任何需要隐式变量名称访问的语言构造。这些内置是语言语法的一部分,因此不允许装饰性额外的括号。
导致运行时错误。
意外
)
缺乏功能参数
您不能有流浪 commas> commas thas thes thes the函数调用。 PHP期望那里有一个价值,因此抱怨早期关闭
)
括号。仅在
array()
或 list()构造中允许tailing逗号。未完成的表达式
如果您忘记了算术表达中的某些东西,那么解析器就会放弃。因为它应该如何解释:
并且,如果您忘记了闭合
)
,即使您会收到关于意外的半隆的抱怨。foreach as
常数
对于 enpotter控制语句您会看到:
php这里有时会告诉您
::
而不是。因为类:: $ variable可以满足预期的$变量表达式。意外的
{
curly braces
{
和}
封闭代码块。它们的语法错误通常表示一些不正确的嵌套。中的无与伦比的子表达
最常见的是不平衡
(
)() /code> 如果解析器抱怨开口卷曲{
出现得太早,则是原因。一个简单的例子:计算您的括号或使用IDE来帮助这一点。也不要在没有任何空间的情况下编写代码。可读性计数。
{and}在表达式上下文中
您不能在表达式中使用卷曲括号。如果您混淆括号和卷发,它将不符合语言语法:
标识符构造有一些例外,例如本地范围变量
$ {core}
。变量变量或卷曲var表达式
这很少见。但是您也可能会得到
{
和}
复杂变量表达式的解析器投诉:尽管在这种情况下出现意外的
}
的可能性更高。出乎意料的
}
获得“出乎意料的
}
”错误时,您大多要过早关闭代码块。代码块中的最后一个语句
任何未终止的表达都可能发生。
,如果函数/代码块中的最后一行缺少尾声
;
semicolon:在这里,解析器无法确定您是否仍然想将
+ 25;
添加到函数结果或其他内容。无效的块嵌套/忘记
{
当代码块为
}
关闭过早时,您有时会看到此解析器错误,或者您忘记了开放{
偶数:在上面的摘要中,如果没有开口
{
curly brace。因此,下面的关闭}
变得多余。因此,用于该功能的下一个关闭}
与原始开口{
curly brace。如果没有适当的代码缩进,就很难找到此类错误。使用IDE和括号匹配。
意外的
{
,期望(
语言构造需要条件/声明标题和 代码块将触发此错误。
参数列表
例如 misdeclared函数,没有参数列表 不允许:
控制语句条件
,您也不能同样拥有
if
if 没有条件P>显然没有意义。对于通常的嫌疑人来说,/
foreach
, while /do
等Unexpected
(
Opening parentheses typically follow language constructs such as
if
/foreach
/for
/array
/list
or start an arithmetic expression. They're syntactically incorrect after"strings"
, a previous()
, a lone$
, and in some typical declaration contexts.Function declaration parameters
A rarer occurrence for this error is trying to use expressions as default function parameters. This is not supported, even in PHP7:
Parameters in a function declaration can only be literal values or constant expressions. Unlike for function invocations, where you can freely use
whatever(1+something()*2)
, etc.Class property defaults
Same thing for class member declarations, where only literal/constant values are allowed, not expressions:
Put such things in the constructor.
See also Why don't PHP attributes allow functions?
Again note that PHP 7 only allows
var $xy = 1 + 2 +3;
constant expressions there.JavaScript syntax in PHP
Using JavaScript or jQuery syntax won't work in PHP for obvious reasons:
When this happens, it usually indicates an unterminated preceding string; and literal
<script>
sections leaking into PHP code context.isset(()), empty, key, next, current
Both
isset()
andempty()
are language built-ins, not functions. They need to access a variable directly. If you inadvertently add a pair of parentheses too much, then you'd create an expression however:The same applies to any language construct that requires implicit variable name access. These built-ins are part of the language grammar, therefore don't permit decorative extra parentheses.
User-level functions that require a variable reference -but get an expression result passed- lead to runtime errors instead.
Unexpected
)
Absent function parameter
You cannot have stray commas last in a function call. PHP expects a value there and thusly complains about an early closing
)
parenthesis.A trailing comma is only allowed in
array()
orlist()
constructs.Unfinished expressions
If you forget something in an arithmetic expression, then the parser gives up. Because how should it possibly interpret that:
And if you forgot the closing
)
even, then you'd get a complaint about the unexpected semicolon instead.Foreach as
constant
For forgotten variable
$
prefixes in control statements you will see:PHP here sometimes tells you it expected a
::
instead. Because a class::$variable could have satisfied the expected $variable expression..Unexpected
{
Curly braces
{
and}
enclose code blocks. And syntax errors about them usually indicate some incorrect nesting.Unmatched subexpressions in an
if
Most commonly unbalanced
(
and)
are the cause if the parser complains about the opening curly{
appearing too early. A simple example:Count your parentheses or use an IDE which helps with that. Also don't write code without any spaces. Readability counts.
{ and } in expression context
You can't use curly braces in expressions. If you confuse parentheses and curlys, it won't comply to the language grammar:
There are a few exceptions for identifier construction, such as local scope variable
${references}
.Variable variables or curly var expressions
This is pretty rare. But you might also get
{
and}
parser complaints for complex variable expressions:Though there's a higher likelihood for an unexpected
}
in such contexts.Unexpected
}
When getting an "unexpected
}
" error, you've mostly closed a code block too early.Last statement in a code block
It can happen for any unterminated expression.
And if the last line in a function/code block lacks a trailing
;
semicolon:Here the parser can't tell if you perhaps still wanted to add
+ 25;
to the function result or something else.Invalid block nesting / Forgotten
{
You'll sometimes see this parser error when a code block was
}
closed too early, or you forgot an opening{
even:In above snippet the
if
didn't have an opening{
curly brace. Thus the closing}
one below became redundant. And therefore the next closing}
, which was intended for the function, was not associable to the original opening{
curly brace.Such errors are even harder to find without proper code indentation. Use an IDE and bracket matching.
Unexpected
{
, expecting(
Language constructs which require a condition/declaration header and a code block will trigger this error.
Parameter lists
For example misdeclared functions without parameter list are not permitted:
Control statement conditions
And you can't likewise have an
if
without condition.Which doesn't make sense, obviously. The same thing for the usual suspects,
for
/foreach
,while
/do
, etc.意外的T_IF
意外的T_elseif
意外的T_ELSE
意外的T_ENDIF
条件控制块
如果
,elseif
和else
遵循简单的结构。当您遇到语法错误时,很可能只是无效的块嵌套→缺少{
curly braces}
- 或一个太多。缺少
{
或}
由于不正确的凹痕不匹配的代码括号对于较不符合良好的代码(例如:
)是常见的
如果您的代码看起来像这样,请重新开始!否则,它对您或其他任何人都无法混合。在Internet上展示此信息以寻求帮助是没有意义的。
您只能在视觉上遵循嵌套的结构和IF/else条件及其
{
代码块}
的关系。使用您的IDE查看它们是否都配对。任何双重
}
}
不仅会关闭分支,而且将关闭先前的条件结构。因此坚持一种编码样式;如果/else树,请勿在嵌套中混合和匹配。除了一致性之外,事实证明,避免冗长的条件也有帮助。 避免不可读
如果
expressions如果
无法在表达式中使用出人意料的新人错误是尝试使用语句在表达式中使用
,例如打印语句:
当然是无效的。
您可以使用一个三元条件,请注意可读性影响。
否则否则会破坏这样的输出构造:使用多个
如果 s和
echo
s。
更好的是,请使用临时变量,然后将条件放置在:< /p>
为这种情况定义功能或方法通常也很有意义。
控制块不返回“结果”
现在这不太常见,但是一些编码器甚至尝试处理
,就好像它可以返回 result :在字符串串联/表达式中使用
在结构上与使用相同。
您必须在代码块中使用分配::
或者,求助于
?:
三元比较。如果在if
中
您如果
< < < /a>在条件下:显然是多余的,因为
和
(或或
)已经允许链接比较。忘记
;
semicolons再次:每个控制块都需要是一个语句。如果先前的代码件未被半柱终止,则是保证的语法错误:
btw,
{…}
代码块中的最后一行也需要一个分号。半隆
现在可能要责怪特定的编码方式是错误的,因为这个陷阱太容易忽略了:
发生的时间比您想象的要多。
if() ;
它将执行一个void语句。;
自己成为一个空的{}
自己!在
else
不再与 construct,这就是为什么这会导致意外的T_ELSE语法错误。
还解释了此语法错误的微妙变化:
;
在代码块{…}
之后终止整个如果
切断
else
分支不使用代码块
句法允许省略卷曲括号
{
…}
中的代码块,如果/elseif> elseif
/<代码> else 分支。遗憾的是,这是一种无关编码器非常常见的语法样式。 (在错误的假设下,这更快地输入或读取)。但是,这很有可能会绊倒语法。迟早的其他语句将进入if/else分支:
但是,要实际使用代码块,您 确实有 写
{
…}
它们这样!其他 / elseif在错误的顺序< / h3>
提醒自己的一件事是有条件的顺序当然。
您可以随意拥有尽可能多的
elseif
s,但是else
必须进行最后。就是这样。类声明
AS 上面提到的,您无法在类声明中具有控制语句:
您要么 t-finction> 下过早关闭一个
}
。意外的T_ELSEIF / T_ELSE < / h3>
混合PHP和HTML时,
的关闭
必须在相同的php块}
如果/elseif&lt;?php?&gt; 作为下一个
> 需要成为elseif/else
。如果elseif
:,这将产生错误::::
正确的表单
&lt;?php} elseif
:这或多或少是错误的压痕变化 - 大概是基于错误的编码意图。
您不能 mash其他语句 如果和
elseif
/else
结构令牌:只能出现在
{…}
代码块中,而不是在控制结构令牌之间。您 a if/else 在不同的控制结构之间:
没有语法关系 之间否则。 结束,因此 结构可以继续。
t_endif
如果抱怨出乎意料的t_endif,则您正在使用替代语法样式
if:
⋯elseif:
⋯else:
⋯<<<代码> endif;
。您应该三思而后行。A common pitfall is confusing the eerily
分配与比较
因此,这不是语法错误,而是在这种情况下值得一提的:
不是
==
//
================= 比较,但
=
分配。这是相当微妙的,很容易引导某些用户无助地编辑整个条件块。首先提防意外任务 - 当您遇到逻辑故障 /不良行为时。< / p>
Unexpected T_IF
Unexpected T_ELSEIF
Unexpected T_ELSE
Unexpected T_ENDIF
Conditional control blocks
if
,elseif
andelse
follow a simple structure. When you encounter a syntax error, it's most likely just invalid block nesting → with missing{
curly braces}
- or one too many.Missing
{
or}
due to incorrect indentationMismatched code braces are common to less well-formatted code such as:
If your code looks like this, start afresh! Otherwise it's unfixable to you or anyone else. There's no point in showcasing this on the internet to inquire for help.
You will only be able to fix it, if you can visually follow the nested structure and relation of if/else conditionals and their
{
code blocks}
. Use your IDE to see if they're all paired.Any double
}
}
will not just close a branch, but a previous condition structure. Therefore stick with one coding style; don't mix and match in nested if/else trees.Apart from consistency here, it turns out helpful to avoid lengthy conditions too. Use temporary variables or functions to avoid unreadable
if
-expressions.IF
cannot be used in expressionsA surprisingly frequent newcomer mistake is trying to use an
if
statement in an expression, such as a print statement:Which is invalid of course.
You can use a ternary conditional, but beware of readability impacts.
Otherwise break such output constructs up: use multiple
if
s andecho
s.Better yet, use temporary variables, and place your conditionals before:
Defining functions or methods for such cases often makes sense too.
Control blocks don't return "results"
Now this is less common, but a few coders even try to treat
if
as if it could return a result:Which is structurally identical to using
if
within a string concatenation / expression.You'll have to use an assignment in the code block:
Alternatively, resort to a
?:
ternary comparison.If in If
You cannot nest an
if
within a condition either:Which is obviously redundant, because the
and
(oror
) already allows chaining comparisons.Forgotten
;
semicolonsOnce more: Each control block needs to be a statement. If the previous code piece isn't terminated by a semicolon, then that's a guaranteed syntax error:
Btw, the last line in a
{…}
code block needs a semicolon too.Semicolon too early
Now it's probably wrong to blame a particular coding style, as this pitfall is too easy to overlook:
Which happens more often than you might imagine.
if ()
expression with;
it will execute a void statement. The;
becomes a an empty{}
of its own!{…}
block thus is detached from theif
, and would always run.else
no longer had a relation to an openif
construct,which is why this would lead to an Unexpected T_ELSE syntax error.
Which also explains a likewise subtle variation of this syntax error:
Where the
;
after the code block{…}
terminates the wholeif
construct, severing the
else
branch syntactically.Not using code blocks
It's syntactically allowed to omit curly braces
{
…}
for code blocks inif
/elseif
/else
branches. Which sadly is a syntax style very common to unversed coders. (Under the false assumption this was quicker to type or read).However that's highly likely to trip up the syntax. Sooner or later additional statements will find their way into the if/else branches:
But to actually use code blocks, you do have to write
{
…}
them as such!Else / Elseif in wrong order
One thing to remind yourself is the conditional order, of course.
You can have as many
elseif
s as you want, butelse
has to go last. That's just how it is.Class declarations
As mentioned above, you can't have control statements in a class declaration:
You either forgot a function definition, or closed one
}
too early in such cases.Unexpected T_ELSEIF / T_ELSE
When mixing PHP and HTML, the closing
}
for anif/elseif
must be in the same PHP block<?php ?>
as the nextelseif/else
. This will generate an error as the closing}
for theif
needs to be part of theelseif
:The correct form
<?php } elseif
:This is more or less a variation of incorrect indentation - presumably often based on wrong coding intentions.
You cannot mash other statements inbetween
if
andelseif
/else
structural tokens:Either can only occur in
{…}
code blocks, not in between control structure tokens.if
andelse
branches.Nor can you part an if/else between different control structures:
There is no syntactic relation between the
if
andelse
. Theforeach
lexical scope ends at}
, so there's no point for theif
structure to continue.T_ENDIF
If an unexpected T_ENDIF is complained about, you're using the alternative syntax style
if:
⋯elseif:
⋯else:
⋯endif;
. Which you should really think twice about.A common pitfall is confusing the eerily similar
:
colon for a;
semicolon. (Covered in "Semicolon too early")As indentation is harder to track in template files, the more when using the alternative syntax - it's plausible your
endif;
does not match anyif:
.Using
} endif;
is a doubled
if
-terminator.While an "unexpected $end" is usually the price for a forgotten closing
}
curly brace.Assignment vs. comparison
So, this is not a syntax error, but worth mentioning in this context:
That's not a
==
/===
comparison, but an=
assignment. This is rather subtle, and will easily lead some users to helplessly edit whole condition blocks. Watch out for unintended assignments first - whenver you experience a logic fault / misbeheviour.出乎意料的$结束
时,当PHP谈论“意外
$ end
”时,这意味着您的代码在解析器期望更多的代码时结束。 (从字面上获取时,该消息有点误导。它与新移民有时假设的变量无关。它指的是“文件的结尾”, eof 。)。关于缺少的
}
卷曲支架几乎总是 ,以关闭前面的代码块。这是解析器期望找到关闭的}
,但实际上达到了文件的末尾。再次使用适当的凹痕避免此类问题。
使用与括号匹配的IDE,以找出
}
不对劲的位置。大多数IDE和文本编辑器中都有键盘快捷键:
大多数IDE也 ive 突出显示匹配的支架,支架和括号。
这使得检查其相关性非常容易:
未终止的表达式
意外$ end $ end $ end 语法/解析器错误也可能发生未终止的表达式或语句:
$ var = func(1,
?&gt;
eof首先查看脚本的结尾。在任何PHP脚本中的最后一个语句中,尾随
;
通常是多余的。但是您应该有一个。正是因为它缩小了这种语法问题。特别是在您发现自己在脚本末尾添加更多语句之后。缩进的Heredoc Markers
另一个常见发生出现,出现字符串。终止标记物被领先的空间,选项卡等忽略:
因此,解析器假设Heredoc字符串继续进行直到文件结束(因此“意外$ end”)。几乎所有的IDE和语法高音编辑都将使这一明显或警告。
ESC的引号标记
如果您在字符串中使用
\
,则具有特殊的含义。这称为“ 逃脱字符“从字面上看下一个角色。回声'
示例: 从字面上看,不是预期的,即作为字符串的一部分而不是关闭字符串的可打印报价。在打开下一个字符串或脚本末尾之后,通常会显示为解析错误。
在Windows中指定路径时非常常见的错误:
“ C:\ XAMPP \ HTDOCS \”
是错误的。您需要“ c:\\ xampp \\ htdocs \\”
。另外,PHP通常会将Unix风格的路径(例如“ C:/XAMPP/HTDOCS/”
)转换为Windows的正确路径。替代语法
在某种程度上有些稀有,您可以在模板中使用替代语法/代码块时看到此语法错误。使用 if:和
else:
以及缺少的endif;
。另请参见:
Unexpected $end
When PHP talks about an "unexpected
$end
", it means that your code ended while the parser is expecting more code. (The message is a bit misleading when taken literally. It's not about a variable named "$end", as sometimes assumed by newcomers. It refers to the "end of file", EOF.)It's pretty much always about a missing
}
curly brace to close preceding code blocks. What it's saying is that the parser is expecting to find a closing}
but actually reached the end of the file.Again, use proper indentation to avoid such issues.
Use an IDE with bracket matching, to find out where the
}
is amiss.There are keyboard shortcuts in most IDEs and text editors:
Most IDEs also highlight matching braces, brackets and parentheses.
Which makes it pretty easy to inspect their correlation:
Unterminated expressions
An
Unexpected $end
syntax/parser error can also occur for unterminated expressions or statements:$var = func(1,
?>
EOFSo, look at the end of scripts first. A trailing
;
is often redundant for the last statement in any PHP script. But you should have one. Precisely because it narrows such syntax issues down. Particularly after you find yourself adding more statements at the end of the script.Indented HEREDOC markers
Another common occurrence appears with HEREDOC or NOWDOC strings. The terminating marker goes ignored with leading spaces, tabs, etc.:
Therefore the parser assumes the HEREDOC string to continue until the end of the file (hence "Unexpected $end"). Pretty much all IDEs and syntax-highlighting editors will make this obvious or warn about it.
Escaped Quotation marks
If you use
\
in a string, it has a special meaning. This is called an "Escape Character" and normally tells the parser to take the next character literally.Example:
echo 'Jim said \'Hello\'';
will printJim said 'hello'
If you escape the closing quote of a string, the closing quote will be taken literally and not as intended, i.e. as a printable quote as part of the string and not close the string. This will show as a parse error commonly after you open the next string or at the end of the script.
Very common error when specifying paths in Windows:
"C:\xampp\htdocs\"
is wrong. You need"C:\\xampp\\htdocs\\"
. Alternately, PHP will usually convert Unix-style paths (e.g."C:/xampp/htdocs/"
) to the correct path for Windows.Alternative syntax
Somewhat rarer you can see this syntax error when using the alternative syntax for statement/code blocks in templates. Using
if:
andelse:
and a missingendif;
for example.See also:
意外的t_is_equal
意外的T_is_greater_or_equal
意外的t_is_istinal
意外的T_IS_NOT_EQUAL
意外的t_is_not_istinal
意外的T_is_smaller_or_equal
意外
&lt;
意外
&gt;
比较运算符,例如
==
,&gt; =
,====
,!=
,,&lt ;应仅在表达式中使用,例如
表达式。如果解析器对它们抱怨,则通常意味着围绕它们周围的parens parens不正确的削皮或不匹配的(
)。
Parens分组
特别是
,如果
带有多个比较的语句,则必须注意正确计数开放和关闭括号:这里的
如果
条件已经由终止)
您的比较变得足够复杂,如果构造而不是
Isset()与比较
捣碎
一个普通的新人是pitfal试图结合
iSSET()
或< a href =“ http://php.net/empty” rel =“ nofollow noreferrer”>empty()
与比较:甚至:
这对PHP没有意义,因为
isset
和empty
是仅接受变量名称的语言构造。比较结果也没有意义,因为输出仅是/已经是布尔值。混淆
&gt; =
更大或平等与=&gt;
数组operator两个操作员看起来有些相似,因此有时会混在一起:
您只需要记住,该比较操作员被称为“ 大于或等于”才能正确。
另请参见: if语句结构中的php
没有任何比较>
,如果它们与相同的变量名称相关,则您也无法结合两个比较:
PHP无法推断出您打算再次比较初始变量。表达通常根据操作员优先
&lt;
可以看到原始变量只剩下布尔结果。另请参阅:意外的t_is_smaller_or_equal
比较链
您无法将变量与一排运算符进行比较:
这必须分解为两个比较,每个比较都与
$ x
。这实际上是黑名单表达式的情况(由于等效运算符协会)。它在几种C风格的语言中在语法上有效,但是PHP也不会将其解释为预期的比较链。
意外
&gt;
意外的
&lt;
大于
&gt;
或小于&lt;
运营商没有自定义t_xxx
tokenizer name。虽然它们像其他所有人一样被放错了位置,但您经常看到解析器抱怨它们的字符串和捣碎的HTML::
这相当于字符串
“&lt; a href ='z”
被比较&gt;
与字面的常数Hello
,然后是另一个&lt;
比较。或者至少是PHP的看法。实际原因和语法错误是“ 终止”。也无法嵌套php启动标签:
另请参阅:
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
>
Comparison operators such as
==
,>=
,===
,!=
,<>
,!==
and<=
or<
and>
mostly should be used just in expressions, such asif
expressions. If the parser complains about them, then it often means incorrect paring or mismatched(
)
parens around them.Parens grouping
In particular for
if
statements with multiple comparisons you must take care to correctly count opening and closing parenthesis: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()
orempty()
with comparisons:Or even:
This doesn't make sense to PHP, because
isset
andempty
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 operatorBoth operators look somewhat similar, so they sometimes get mixed up:
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:
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:
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 customT_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:This amounts to a string
"<a href='z"
being compared>
to a literal constantHello
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:
See also:
意外的T_IF
意外的T_foreach
出乎意料的t_for
意外的T_时
意外的T_DO
意外的T_echo
控制构造,例如,,, for , while ,,
list
,<代码>全局,<代码>返回,<代码> do ,print
,echo
只能用作语句。他们通常自己居住在一条线上。semicolon;你在哪里?
非常普遍地让您错过了一个半元素如果解析器抱怨,则在上一行中关于控制语句:
解决方案:查看上一行;添加分号。
类声明
发生这种情况的另一个位置是班级声明。在课程部分中,您只能列出属性初始化和方法部分。没有代码可以存放在那里。
这种语法错误通常是错误嵌套的
{
和}
的实现。特别是当功能代码块过早关闭时。表达式上下文中的语句
大多数语言构造都可以仅用用作语句 。它们并不是要放置在其他表达式中:
同样,如果在字符串,数学表达式或其他地方使用,也不能使用
:
用于嵌入
的
类似于表达式中的条件,您经常想使用?:
三元评估。echo
和较小的扩展list列表,
同样适用于, 。print()
是一种内置的语言,可以在表达式上下文中使用。 (但很少有意义。)保留的关键字作为标识符
如果 以及用户定义的函数或类名称的其他语言构造,您也不能使用或
。 (也许在php&nbsp; 7中。但即使那样也不建议。)
您的控制块后,您有一个半彩色,而不是结肠(:)或curly bracket(:)或curly bracket({){)
控制结构通常包裹在卷曲括号中(但结肠可以在替代语法)表示其范围。如果您不小心使用半彩色,则过早地关闭该块,导致您的关闭语句抛出错误。
Unexpected T_IF
Unexpected T_FOREACH
Unexpected T_FOR
Unexpected T_WHILE
Unexpected T_DO
Unexpected T_ECHO
Control constructs such as
if
,foreach
,for
,while
,list
,global
,return
,do
,print
,echo
may only be used as statements. They usually reside on a line by themselves.Semicolon; where you at?
Pretty universally have you missed a semicolon in the previous line if the parser complains about a control statement:
Solution: look into the previous line; add semicolon.
Class declarations
Another location where this occurs is in class declarations. In the class section you can only list property initializations and method sections. No code may reside there.
Such syntax errors commonly materialize for incorrectly nested
{
and}
. In particular when function code blocks got closed too early.Statements in expression context
Most language constructs can only be used as statements. They aren't meant to be placed inside other expressions:
Likewise can't you use an
if
in strings, math expressions or elsewhere:For embedding
if
-like conditions in an expression specifically, you often want to use a?:
ternary evaluation.The same applies to
for
,while
,global
,echo
and a lesser extendlist
.Whereas
print()
is a language built-in that may be used in expression context. (But rarely makes sense.)Reserved keywords as identifiers
You also can't use
do
orif
and other language constructs for user-defined functions or class names. (Perhaps in PHP 7. But even then it wouldn't be advisable.)Your have a semi-colon instead of a colon (:) or curly bracket ({) after your control block
Control structures are typically wrapped in curly braces (but colons can be used in an alternative syntax) to represent their scope. If you accidentally use a semi-colon you prematurely close that block resulting in your closing statement throwing an error.
意外的?''
如果您尝试在
&lt;?php
中使用&lt;?php
,则将给出此错误。*用于PHP版本4.3.1、4.3.5-4.3.11、4.4.0-4.4.1、5.0.0-5.0.5、4.4.4.2-4.4.2-4.4.9、5.1.0-5.1.0-5.1.6 ,5.2.0-5.2.17,5.3.0-5.3.29,5.4.0-5.4.45,5.5.0-5.5.38,5.6.0-5.6.40,7.0.0.0.0.0-7.0.0.33,7.1 .0-7.1.33,7.2.0-7.2.34,7.3.0-7.3.31,7.4.0-7.4.4-7.4.24
如果您试图使用null ColeScing Operator
?? < /代码>在PHP 7之前的PHP版本中,您将获得此错误。
出乎意料的“?”,期望变量
对于无效类型可能会出现类似的错误,如:
再次指示正在使用过时的PHP版本(CLI版本
php -v -v
或WebServer BOND ANE BOND ANEphpinfo();
)。Unexpected '?'
If you are trying to use
<?php
within<?php
this error will be given*.* For PHP versions 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5, 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.31, 7.4.0 - 7.4.24
If you are trying to use the null coalescing operator
??
in a version of PHP prior to PHP 7 you will get this error.Unexpected '?', expecting variable
A similar error can occur for nullable types, as in:
Which again indicates an outdated PHP version being used (either the CLI version
php -v
or the webserver bound onephpinfo();
).意外的t_lnumber
令牌
t_lnumber < / code>是指“长” /编号。
无效的变量名称
在php和大多数其他编程语言中,不能以一个数字开头。第一个字符必须是字母或下划线。
经常出现用于使用
preg_replace
-placeholder“ $ 1”
在PHP上下文中:应该引用回调的地方。 (现在已弃用了
/e
REGEX标志。相同的标识符约束适用于对象属性,btw。
虽然令牌/解析器不允许文字
$ 1
作为变量名称,但一个可以 使用$ {1}
或$ {“ 1”}
。这是非标准标识符的句法解决方法。 (最好将其视为本地示波器查找。但是通常:在这种情况下更喜欢普通阵列!)有趣的是,但不推荐使用,PHPS解析器允许Unicode-sidentifiers;因此
$➊
是有效的。 (与文字1
不同)。流浪阵列条目
Array声明也可能发生意外的长时间。 - 缺少
,
逗号:或同样的函数调用和声明以及其他构造:
func(1,2 3);
函数xy($ z 2);
($ i = 2 3&lt; $ z)
因此,通常有一个
;
或,用于分离列表或表达式的缺少。错误引用的HTML
再次,误引用的字符串数字:
这种情况应或多或少地像出乎意料的T_STRING 错误错误。
其他标识符
既不函数,类,也不是可以以一个数字开始命名:
与变量名称几乎相同。
Unexpected T_LNUMBER
The token
T_LNUMBER
refers to a "long" / number.Invalid variable names
In PHP, and most other programming languages, variables cannot start with a number. The first character must be alphabetic or an underscore.
Quite often comes up for using
preg_replace
-placeholders"$1"
in PHP context:Where the callback should have been quoted. (Now the
/e
regex flag has been deprecated. But it's sometimes still misused inpreg_replace_callback
functions.)The same identifier constraint applies to object properties, btw.
While the tokenizer/parser does not allow a literal
$1
as variable name, one could use${1}
or${"1"}
. Which is a syntactic workaround for non-standard identifiers. (It's best to think of it as a local scope lookup. But generally: prefer plain arrays for such cases!)Amusingly, but very much not recommended, PHPs parser allows Unicode-identifiers; such that
$➊
would be valid. (Unlike a literal1
).Stray array entry
An unexpected long can also occur for array declarations - when missing
,
commas:Or likewise function calls and declarations, and other constructs:
func(1, 2 3);
function xy($z 2);
for ($i=2 3<$z)
So usually there's one of
;
or,
missing for separating lists or expressions.Misquoted HTML
And again, misquoted strings are a frequent source of stray numbers:
Such cases should be treated more or less like Unexpected T_STRING errors.
Other identifiers
Neither functions, classes, nor namespaces can be named beginning with a number either:
Pretty much the same as for variable names.
意外'='
这可能是由于在变量名称中具有无效的字符而引起的。变量名称必须遵循以下规则:
Unexpected '='
This can be caused by having invalid characters in a variable name. Variables names must follow these rules:
意外的“最终”(t_endile)
语法正在使用结肠 - 如果没有结肠,则会发生上述错误。
该语法的替代方法是使用卷曲括号:
http:/ http://php.net/手动/en/en-control structures.while.php
Unexpected 'endwhile' (T_ENDWHILE)
The syntax is using a colon - if there is no colon the above error will occur.
The alternative to this syntax is using curly brackets:
http://php.net/manual/en/control-structures.while.php
出乎意料的'。
如果您尝试使用 splat Operator(
...
)不支持的PHP版本。...
首先在PHP 5.6中可用,以捕获一个函数的可变数量参数:在PHP 7.4中,您可以将其用于 array表达式。
Unexpected '.'
This can occur if you are trying to use the splat operator(
...
) in an unsupported version of PHP....
first became available in PHP 5.6 to capture a variable number of arguments to a function:In PHP 7.4, you could use it for Array expressions.
意外
:
1。PHP8名称参数语法
如果在PHP&lt版本中使用PHP 8的新名称参数功能; 8此错误将发生:
解决方案:
2。 :: 分隔符
开始解析错误的错误消息:语法错误,意外':'可能是通过错误地编写类静态参考
class :: $ variable
as类而引起的。 $变量
。Unexpected
:
1. PHP 8 named parameter syntax
If attempting to use PHP 8's new named parameter functionality in a version of PHP < 8 this error will occur:
Solutions:
2. Cropped class
::
separatorAn error message that begins Parse error: syntax error, unexpected ':' can be caused by mistakenly writing a class static reference
Class::$Variable
asClass:$Variable
.意外的“继续”(t_continue)
继续
是一个语句(例如,或者),并且必须独立出现。它不能用作表达的一部分。部分是因为继续返回值,但是在表达式中,每个子表达都必须导致某种值,因此总体表达式会导致值。这就是陈述和表达式之间的区别。这意味着
继续
不能在三元语句或任何需要返回值的语句中使用。出乎意料的“断裂”(t_break)
也是
Break;
当然也是如此。它在表达上下文中也不是可用的,而是严格的语句(在 block的同一级别上)。意外的“返回”(t_return)
现在对于
返回
来说可能更令人惊讶,但这也只是一个块级语句。它确实将值(或null)返回到更高的范围/函数中,但不会评估为表达式本身。 →那就是:做return(return(false);;
Unexpected 'continue' (T_CONTINUE)
continue
is a statement (like for, or if) and must appear standalone. It cannot be used as part of an expression. Partly because continue doesn't return a value, but in an expression every sub-expression must result in some value so the overall expression results in a value. That's the difference between a statement and an expression.That means
continue
cannot be used in a ternary statement or any statement that requires a return value.Unexpected 'break' (T_BREAK)
Same goes for
break;
of course. It's also not usable in expression context, but a strict statement (on the same level asforeach
or anif
block).Unexpected 'return' (T_RETURN)
Now this might be more surprising for
return
, but that's also just a block-level statement. It does return a value (or NULL) to the higher scope/function, but it does not evaluate as expression itself. → That is: there's no point in doingreturn(return(false);;
发生这些错误的另一个原因是像带有代码的类似字符一样出乎意料的空格,代码行似乎是完美的,但是它们包含一些类似于Break Line或Whitespace或Whitespace或Tab的特定字符解析器。
当我尝试通过简单地复制粘贴将一些代码从网页中放置到代码编辑器时,我会面临这个问题,我看到了带有数组定义的错误。在数组定义中,一切都看起来正确。我无法整理正确的错误,最后我将此数组定义为单行,然后错误就消失了。然后,我再次尝试使该定义多个类似,但要手动为每个数组元素添加休息(Enter),并保存了该文件,这次没有编辑器解析错误,也没有错误的错误。
例如,我在一个博客上遇到了这个片段问题,实际上无法发布这些片段,因为Stack Overflow已经知道代码的问题。
然后解决了我的工作片段后,它看起来与显示解析错误
语法错误,意外的'auth''(t_constant_encapsed_string)相似,期待']'
One more reason to occurrence of these errors is unexpected whitespace like similar characters with-in code, the code lines seems to be perfect, but they contains some specific characters which are similar to break line or whitespace or tab but they not get parsed by the parser.
I face this issue when I try to put some code from webpage to the code editor by simply copy paste, I saw this error with array definition. everything was looking right in array definition. I can't sort out right error, finally I define this array in single line, then error was gone. then again I try to make that definition multiple like but manually adding break(Enter) for each array element and saved the file this time no parsing error by editor and also no error while running it.
For Example I faced issue with this snippet which was on one blog, actually can't post those snippets ,cause stack overflow already knows the problem with code.
then after solving it my working snippet is, which looks similar with one which shows parsing error
syntax error, unexpected ''auth'' (T_CONSTANT_ENCAPSED_STRING), expecting ']'
对于Newbies到VS代码,如果您看到语法错误,请检查是否保存了该文件。如果您的语法错误,保存文件,然后再次修复语法WISHOU再次保存,VS代码将继续显示错误。仅在保存文件后,错误消息才会消失。
For newbies to VS Code, if you see the syntax error, check if you have saved the file. If you have a wrong syntax, save the file, and then fix the syntax withou saving again, VS Code will keep showing you the error. The error message will disappear only after you save the file.
IDE是必须避免SINTAX错误的强制性的,但是诸如PHPSTAN或PHPC之类的工具也必须确保您以团队同事和代码相同的方式进行编码。
如果没有这些工具(或您的同事),只需更改几行时,您就无法在GitHub评论中找到大量的更改。
您也可以配置承诺,以便在承诺确保一切正常(紧张地说)之前与其他其他工具一起运行这些工具。
An IDE is mandatory to avoid sintax error, but tools like PHPStan or PHPCS are also mandatory to be sure you are coding in the same way your team colleagues ande your code is well indented.
Without this tools you (or your colleagues) could find tons of changes in github reviews when just a few lines were changed.
You can configure your commits as well to run these tools with others like CSSLint or ESLint before committing to be sure everything's fine (sintatically speaking).