Posts Tagged ‘web dev’

PHP: Using Switch to test Multiple Conditions

Sunday, August 3rd, 2008

Ok, so maybe this is common knowledge, but I still felt a little clever when I figured out this trick.  The situation was that I wanted to test a bunch of different conditions to figure out which time category a date falls into.  It would be fairly simple to do with a bunch of if…then…else’s, but really ugly.  Here’s the trick I came up with…use switch(true), and then set up your cases to evaluate to true or false. Here’s some example code:

(more…)