PHP Zone

How to use a switch case in PHP?

2 Nov , 2015  

Introduction to Switch Cases :

The switch case is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

Usage of Switch Cases :

With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation!

The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.

Possible Switch Cases :

  • (i). A simple switch statement :

    The switch statement is wondrous and magic. It’s a piece of the language that allows you to select between different options for a value, and run different pieces of code depending on which value is set.
    Each possible option is given by a case in the switch statement.

    Example :

    switch($bar)
    {
        case 4:
            echo "This is not the number you're looking for.\n";
            $foo = 92;
    }
    
  • (ii). Delimiting code blocks :

    The major caveat of switch is that each case will run on into the next one, unless you stop it with break. If the simple case above is extended to cover case 5:

    Example :

    case 4:
        echo "This is not the number you're looking for.\n";
        $foo = 92;
        break;
    
    case 5:
        echo "A copy of Ringworld is on its way to you!\n";
        $foo = 34;
        break;
    
  • (iii). Using fallthrough for multiple cases :

    Because switch will keep running code until it finds a break, it’s easy enough to take the concept of fallthrough and run the same code for more than one case:

    Example :

    case 2:
    case 3:
    case 4:
        echo "This is not the number you're looking for.\n";
        $foo = 92;
        break;
    
    case 5:
        echo "A copy of Ringworld is on its way to you!\n";
        $foo = 34;
        break;
    
  • (iv). Advanced switching: Condition cases :

    PHP’s switch doesn’t just allow you to switch on the value of a particular variable: you can use any expression as one of the cases, as long as it gives a value for the case to use. As an example, here’s a simple validator written using switch:

    Example :

    switch(true)
    {
        case (strlen($foo) > 30):
            $error = "The value provided is too long.";
        $valid = false;
        break;
    
        case (!preg_match('/^[A-Z0-9]+$/i', $foo)):
            $error = "The value must be alphanumeric.";
        $valid = false;
        break;
    
        default:
        $valid = true;
        break;
    }
    
John Peter
I’m professional web developer with 6+ years experience. My wide range of knowledge in web development using PHP, jQuery, WordPress, Magento, Symfony, Smarty and Web Scrapping. I am working with strong enthusiastic team (Group of friends) with spirit. So we provide all web development related solutions like HTML/CSS development, Web Graphic design and LOGO.

, , , , , ,


badge