Using Expressions with PHP’s Switch Statement

Published on April 1, 2013 by

In this short post, I will show you a little trick with the switch statement in PHP – one that many people are not aware of. Normally you will see something like the following:

$number = 5;

switch ($number) {
	case 5: /* Do something */ break;
	case 10: /* Do something */ break;
	
	default: /* Do something */ break;
}

This will most likely be very familiar to you. As is turns out, however, it is also possible to use expressions with the switch statement. For instance, we could rewrite the above example to the below.

$number = 5;

switch (true) {
	case ($number == 5): /* Do something */ break;
	case ($number == 10): /* Do something */ break;
	
	default: /* Do something */ break;
}

Logical, isn’t it? We simply put in a value of true (in this example at least) and then the case with an expression which evaluates to true will be executed. The above example is obviously a silly one, though.

Chances are that you will not use this and off the top of my head, I cannot think of a use case where it makes a lot of sense (that is not to say that it does not exist!). However, one day I just thought if this was possible, and when I tried it out and saw it working, I thought that it was pretty cool. Therefore I just wanted to share it regardless of whether it will be used or not.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

5 comments on »Using Expressions with PHP’s Switch Statement«

  1. Peter

    I have found this useful for when a variable could be one of a number of different types and you need to do something different depending on the type. Or you have an array with a bunch of different types.

    It beats having a whole rake of if($thingy instanceof $that) or if(is_something($thingy)) statements

  2. Carlos

    Great looking website!
    Came here after trying to find out why mixing cases isn’t working for me in PHP/Codeigniter
    like so:

    $numofrows = $this->db->count_all_results();

    switch ($numofrows) {

    case($numofrows>2) return ‘too many images’; break;
    case (2): return ‘has two images’; break;
    case (1): return ‘has one image’; break;
    case (0): return ‘can upload two images’; break;
    default: return ”; break;
    }

    It happens that if $numofrows is 0, I get ‘too many images’, but if I move that case to the bottom, everything works.. So I’m on my way to hunting for the logic.. just commenting to say Good Looking website.
    Greetings from the Florida area.

    • Hello Carlos,

      Thank you for your comment. I would recommend not mixing expressions with the “typical cases” like you did. I think you would have more predictable results if you changed all of your cases to be expressions, e.g. case ($numofrows == 2) etc. I guess you can make it work with a mixture as you said, but in your particular case, I would just pull the expression out of the switch as an if-statement. It depends on the number of cases within the switch, but in your particular case, that would be my recommendation.

      Thank you for sharing this! :-)

  3. TMI

    The reason that Carlos’s code returns ‘too many images’ if $numofrows is 0, is because PHP does a loose comparison for Switch statements.

    Therefore, when $numofrows is 0 (i.e. FALSE), the first matching condition is ($numofrows > 2), because this is FALSE when $numofrows is 0

    The reason the code works when
    case($numofrows>2) return ‘too many images’; break;
    is moved to the bottom is because the first matching condition will then be:
    case (0): return ‘can upload two images’; break;

  4. Jeffrey Simon

    There is a great use case for this idiom; namely when each case condition can be a complex condition. For example case ($x ==1 && $y == 2), Of course you can put arbitrarily complex conditions in each case.

Leave a Reply

Your e-mail address will not be published.