Fall-through Switch Case with For Loop PHP -
please help.
for($i=0; $i<12; $i++ ){ switch($i) { case 0: case 1: case 2: case 3: case 4: echo ("i less 5 <br>"); break; case 5: case 6: case 7: case 8: case 9: echo ("i less 10 <br>"); break; default: echo ("i 10 or more <br>"); } }
this example code got in java book , translated code above php.
the output of following code:
i less 5 less 5 less 5 less 5 less 5 less 10 less 10 less 10 less 10 less 10 10 or more 10 or more
my question how come case 0 case 3 outputs "i less 5" though doesn't have following code , case 4 1 echo statement? i'm confused, can explain me. in advance.
that's how switch
supposed operate. in order stop falling through next case have use break
keyword. it's same in every language know, including javascript, php , python.
for reference, check out php manual.
Comments
Post a Comment