Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Programming

php switch/case statements

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.

Leave a Reply