我最近遇到了
http://php.net/manual/en/control-structures.alternative-syntax.php中描述的switch语句语法错误
我的IDE(PHPstorm)检测到错误,但它没有提供任何有用的纠正上下文.将文件作为模板包含时,代码肯定会产生致命错误.
手册页的警告:
Warning Any output (including whitespace) between a switch statement and the first case will result in a Syntax error. For example,this is invalid:
<?PHP switch ($foo): ?> <?PHP case 1: ?> ... <?PHP endswitch ?>
Whereas this is valid,as the trailing newline after the switch statement is considered part of the closing ?> and hence nothing is output between the switch and case:
<?PHP switch ($foo): ?> <?PHP case 1: ?> ... <?PHP endswitch ?>
手册页没有提供任何解释.一些用户comments on the page也没有解释任何事情;他们只是重申不允许有空格.
为什么这是语法错误?
它就是.
原文链接:https://www.f2er.com/php/131994.html这是一个语法错误,原因与此相同:
<?PHP $foo = 1; switch ($foo) { ?> This can't be here. <?PHP case 1: echo "I'm one"; break; case 2: echo "I'm two"; break; }
这导致:
[27-Jan-2016 22:21:08 Europe/Berlin] PHP Parse error: Syntax error,unexpected ‘ This can’t be here.’,expecting case (T_CASE) or default (T_DEFAULT) or ‘}’ in /path/file on line 7
唯一可以跟随转换的是一个案例.这就是语言的运作方式.
使用替代语法的空白特定限制是它是替代语法的原因之一:它导致难看的格式化并且缺少通常期望看到它的缩进.