Saturday, October 27, 2012

PHP : Switch statement with multiple cases

In PHP programming, sometimes we encounter in a conditions which we have to make a switch statement with multiple case values​​. Previously I was very confused how to make it, but after a bit of experimenting and googling anywhere, I finally find the way. See the code below for more details:
<?

# PHP    : switch with multiple case
# Author : adi@riswan.com

echo "| X | Y | ";
echo "--------- ";

for ($x=1;$x<=5;$x++)
 {
  switch ($x) 
  {
   case 1 :
   case 4 : $y = 'A';
      break;
   case 2 :
   case 5 : $y = 'B';
      break;
   case 3 : $y = 'C';
      break;
  }
 echo "| $x | $y | ";
 }
?>
and the result will be :
| X | Y |
---------
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | A |
| 5 | B |

No comments:

Post a Comment

Leave your comments with attitude please. This blog does DOFOLLOW :-)