I am trying to learn switch case
php code. Here is the program that is working fine when using break.
for ($i=1;$i<=100;$i++) {
switch(true) {
case ( $i%5 == 0 && $i%3 == 0 ):
print 'foobar';
break;
case ( $i%3 == 0 ):
print 'foo';
break;
case ( $i%5 == 0 ):
print 'bar';
break;
case ( $i%5 != 0 && $i%3 != 0 ):
print $i;
break;
}
echo '<br>';
}
But when I use the following code, it is giving me unexpected results:
for ($i=1;$i<100;$i++) {
switch(true) {
case ( $i%3 == 0 ):
print 'foo';
case ( $i%5 == 0 ):
print 'bar';
default:
print $i;
}
echo '<br>';
}
What is wrong in the second example?? Will default
executes even when any above case is executed?? Also why $i%5
case is running for when $i
equals to 3??