phpswitch-statementphp-parse-error

Why Error if open and close PHP tags between switch and first case statement?


Works:

    <?php switch ($student[$use_grade]) {
        case "K": ?>
            <?php echo 'works'; ?>
        <?php break; ?>
    <?php } ?>

Doesn't work:

    <?php switch ($student[$use_grade]) { ?>
        <?php case "K": ?>
            <?php echo 'works'; ?>
        <?php break; ?>
    <?php } ?>

Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_CASE or T_DEFAULT or '}'


Solution

  • From comments documentation of 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