微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 为什么alternate-syntax switch语句中的输出会导致语法错误

我最近遇到了 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也没有解释任何事情;他们只是重申不允许有空格.

为什么这是语法错误

它就是.

这是一个语法错误,原因与此相同:

<?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

唯一可以跟随转换的是一个案例.这就是语言的运作方式.

使用替代语法的空白特定限制是它是替代语法的原因之一:它导致难看的格式化并且缺少通常期望看到它的缩进.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐