This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Control Flow: Switch Forum

View Course » View Exercise

729 points
516849e64ce763a06c0054d7_385249007
Submitted by
Marlysson Silva
over 4 years ago

What's wrong in the code?

<!DOCTYPE html>

<?php
$i = 5;

switch ($i):
case 0:
echo '$i is 0.';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
echo '$i is somewhere between 1 and 5.';
break;
case 6:
case 7:
echo '$1 is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is.";
endswitch;
}
?>

What's wrong in the code? Him not running


10 votes

permalink

The correct code is :

<!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
        <?php
    $i = 5;

    switch ($i):
    case 0:
    echo '$i is 0.';
    break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    echo '$i is somewhere between 1 and 5.';
    break;
    case 6:
    case 7:
    echo '$1 is either 6 or 7.';
    break;
    default:
    echo "I don't know how much \$i is.";
    endswitch;
    }
    ?>
        </body>
    </html>

1976 points
4878c956dffea53b99f36518ab7fed80?s=140&d=retro
Submitted by
ChippeRock
about 4 years ago

7 Comments

51fd3dc280ff33b0e5004064_32411941 Black Ops 2 about 4 years ago

this code is correct

51fd3dc280ff33b0e5004064_32411941 Black Ops 2 about 4 years ago

1v1 me black ops 2.. diamond everything........FTB............

23194a3fb83df7bc38affa431c7cbc53?s=140&d=retro Rey Mendes over 3 years ago

<!DOCTYPE html>





<?php
$i = 5;

switch ($i):
    case 0:
        echo '$i is 0.';
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        echo '$i is somewhere between 1 and 5.';
        break;
    case 6:
    case 7:
        echo '$i is either 6 or 7.';
        break;
    default:
        echo "I don't know how much \$i is.";
endswitch;
?>
</body>

533c490b8c1cccfd9c0016e0_783862310 HellyPatel01 over 2 years ago

<!DOCTYPE html>





<?php
$i = 5;

switch ($i):
case 0:
echo '$i is 0.';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
echo '$i is somewhere between 1 and 5.';
break;
case 6:
case 7:
echo '$1 is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is.";
endswitch;
}
?>
    </body>
</html>

533c490b8c1cccfd9c0016e0_783862310 HellyPatel01 over 2 years ago

it worked for me

533c490b8c1cccfd9c0016e0_783862310 HellyPatel01 over 2 years ago

THANKS

4878c956dffea53b99f36518ab7fed80?s=140&d=retro ChippeRock almost 2 years ago

Your welcome.


3 votes

permalink

Try removing the curly bracket punctuation } after the endswitch; and see if the error still persists.

1068 points
50406dc5a35538000201b210_185893899
Submitted by
xylentangel
over 4 years ago


2 votes

permalink

made some changes and it worked as follows

<?php
$i = 5;

switch ($i):
    case 0
        echo '$i is 0.';
        break;
    case 1
    case 2
    case 3
    case 4
    case 5
        echo '$i is somewhere between 1 and 5.';
        break;
    case 6
    case 7
        echo '$i is either 6 or 7.';
        break;
    default:
        echo "I don't know how much \$i is.";
endswitch;
?>

just remove the : (colon) from the case and try it worked for me.!!!!!

939 points
579c5539836966759e000002_431696142
Submitted by
sniperk6
about 3 years ago

2 Comments

B13465219263fa8ce885889a9f77a94b?s=140&d=retro Shnikers about 3 years ago

thanks a lot dude!

Picture Muhammad Arsalan about 2 years ago

which colon you're talking about mate ?


0 votes

permalink

<!DOCTYPE html>





<?php
$i = 5;

switch ($i) ;
    case 0:
        echo '$i is 0.';
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        echo '$i is somewhere between 1 and 5.';
        break;
    case 6:
    case 7:
        echo '$i is either 6 or 7.';
        break;
    default:

        echo "I don't know how much \$i is.";
endswitch;
?>
</body>

my coding looks right but i can't pass the exercise what am i suppose to do?

792 points
52ac392e548c35101c001922_979903564
Submitted by
Shine
almost 4 years ago


0 votes

permalink

Perhaps you should try the following code:

<!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
        <?php
    $i = 5;

    switch ($i) :
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$1 is either 6 or 7.';
            break;
        default:
            echo 'I don\'t know how much \$i is';
            break;
    endswitch;
    ?>
        </body>
    </html>   

1976 points
4878c956dffea53b99f36518ab7fed80?s=140&d=retro
Submitted by
ChippeRock
almost 4 years ago


0 votes

permalink

add after endswitch; a "}" dont ask me why, they need it, if they say we need to remove it, neither does the code needs it, imo -_-'

994 points
5152e919cac3e77c3f000606_201119768
Submitted by
Aivars Zuk
about 3 years ago


0 votes

permalink

The } suggested above works - or just adding an extra line betweenendswitch; and ?>. (That way, it still runs properly.)

793 points
53d2826e7c82cac4b40000c7_344082374
Submitted by
Benjamin Wyatt
about 3 years ago


0 votes

permalink

<!DOCTYPE html>

<?php
$i = 5;

switch ($i):
    case 0:
        echo '$i is 0.';
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        echo '$i is somewhere between 1 and 5.';
        break;
    case 6:
    case 7:
        echo '$i is either 6 or 7.';
        break;
    default:
        echo "I don't know how much \$i is.";
endswitch
?>
</body>

591 points
548473d386f552262a000e72_412782804
Submitted by
Kingsley
almost 3 years ago