为什么在 switch 和第一个 case 语句之间打开和关闭 PHP 标签会出错?
Why Error if open and close PHP tags between switch and first case statement?
作品:
<?php switch ($student[$use_grade]) {
case "K": ?>
<?php echo 'works'; ?>
<?php break; ?>
<?php } ?>
无效:
<?php switch ($student[$use_grade]) { ?>
<?php case "K": ?>
<?php echo 'works'; ?>
<?php break; ?>
<?php } ?>
解析错误:语法错误,意外 T_INLINE_HTML,应为 T_CASE 或 T_DEFAULT 或“}”
来自 PHP 的评论文档:
in the case of the switch statement, can be understood as follows; in
any place where you can have an echo statement (an if block, a
switch's case, whatever), that's where you can have the raw HTML. In
PHP this basically gets handled just like that -- like an echo
statement.
In between a switch and a case, though, you can't echo anything. By
placing the switch and the case in two separate blocks of PHP, with a
raw HTML newline echo'ed in between them, PHP basically had to try to
find where that statement would be. And it can't be there, hence the
difficulty.
http://www.php.net/manual/en/control-structures.alternative-syntax.php
内联 html(?>
和 <?php
之间的所有内容)在语法上与运算符相同,并且 switch
和第一个 [=13] 之间不允许使用运算符=].
来自 PHP 文档:
Any output (including whitespace) between a switch statement and the
first case will result in a syntax error.
作品:
<?php switch ($student[$use_grade]) {
case "K": ?>
<?php echo 'works'; ?>
<?php break; ?>
<?php } ?>
无效:
<?php switch ($student[$use_grade]) { ?>
<?php case "K": ?>
<?php echo 'works'; ?>
<?php break; ?>
<?php } ?>
解析错误:语法错误,意外 T_INLINE_HTML,应为 T_CASE 或 T_DEFAULT 或“}”
来自 PHP 的评论文档:
in the case of the switch statement, can be understood as follows; in any place where you can have an echo statement (an if block, a switch's case, whatever), that's where you can have the raw HTML. In PHP this basically gets handled just like that -- like an echo statement.
In between a switch and a case, though, you can't echo anything. By placing the switch and the case in two separate blocks of PHP, with a raw HTML newline echo'ed in between them, PHP basically had to try to find where that statement would be. And it can't be there, hence the difficulty.
http://www.php.net/manual/en/control-structures.alternative-syntax.php
内联 html(?>
和 <?php
之间的所有内容)在语法上与运算符相同,并且 switch
和第一个 [=13] 之间不允许使用运算符=].
来自 PHP 文档:
Any output (including whitespace) between a switch statement and the first case will result in a syntax error.