PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 13, 2022

[FIXED] PHP switch statement problem causes a syntax error

 January 13, 2022     php, php-7.4, switch-statement     No comments   

Issue

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:\xampp\htdocs\php-mvc\public\test.php on line 5

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


Solution

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
    }
?>


Answered By - thephper
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing