This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by Heqing Wong
about 11 years

Why i can't run it?

Answer 515afb653552daad540012de

9 votes

Permalink

you have a too high error in your syntax, see my code of example! below:

<?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;
?>
points
Submitted by Sergio A
about 11 years

3 comments

Kareem Damen about 11 years

thx!

Heqing Wong about 11 years

I know this is right ,but i want to use the code that named “falling through”

Dan Reinders about 11 years

In the code in this answer case 1, case 2, case 3, and case 4 all “Fall Through” to the code in case 5. At the end of the code in case 5 the break statement ends the execution of the switch statement for those cases.

case 6 falls through to case 7 and then the break ends the execution of the switch statement for those cases.

Answer 515c4366747ca6a6ab00008b

0 votes

Permalink

It doesnt work because $i == 5. And you don’t have case 5: in your code.

You only have case 2 and then your code skips to the situation when $i == 6

And what are you trying to acomplish with that code? Translated into english it would be like:

$i is 2
if $i is 2 chek if $i is either 3, 4, 5 and, if it isn't, do nothing!

It is obvious that, as long as $i == 2, it can’t simoultaneusly be 3, 4 or 5.

points
Submitted by Andrei Ionita
about 11 years

Answer 5205bbb6abf82173b5001b4d

0 votes

Permalink

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;

points
Submitted by carlosmante
over 10 years