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

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

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...