switch ($i)
{
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
Update: I have a few developers at my work that have a java background and they didn’t know that you can actually include a conditional in your case, cause I guess java doesn’t let you. So for case 1 (in this instance) you could actually have it test against a variable outside the switch so it would read:
$var = 2;
switch ($i)
{
case 0:
echo "i equals 0";
break;
case $i == $var:
echo "i equals 1";
break;
case $i== $var:
echo "i equals 2";
break;
}
I forget where I used this code recently, but it is useful, as it makes your switch more dynamic.
No related posts.