加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:40:43 所属栏目:PHP教程 来源:网络整理
导读:我最近遇到了 http://php.net/manual/en/control-structures.alternative-syntax.php中描述的switch语句语法错误 我的IDE(phpstorm)检测到错误,但它没有提供任何有用的纠正上下文.将文件作为模板包含时,代码肯定会产生致命错误. 手册页的警告: Warning Any
我最近遇到了 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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读