Skip to content
Advertisement

PHP switch statement problem causes a syntax error [closed]

My code:

<?php
    $i = 1;
    switch ($i) {
?>
        
        <?php
            case 1:
        ?>
        $i is 1
        <?php
            break;
        ?>
<?php
    }
?>

This code gives me an error:

Parse error: syntax error, unexpected ' ', expecting case (T_CASE) or default (T_DEFAULT) or '}' in D:xampphtdocsphp-mvcpublictest.php on line 5

I know that I can avoid closing php tags and say echo 'text'; but still, how to fix this?

Advertisement

Answer

You are breaking the switch syntax off by outputting empty spaces when closing and opening the PHP tag.

This will work:

<?php
    $i = 1;
    switch ($i) {
            case 1:
        ?>
        $i is 1
        <?php
            break;
        ?>
<?php
    }
?>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement