Switch Ranges in PHP
You have to admit it.
The switch statement in PHP vis-a-vis to other language counter parts is very versatile and powerful.
It has support for switching integer as well as characters and strings.
Further, as I discovered it the other day, it also supports ranges !.
[code lang="php"]
switch(true)
{
case ($i==6):
doSomething();
break;
case ($i >= 8 && $i <= 17):
rangedData();
break;
}
[/code]
So basically, the 'case' statements can evaluate conditions. Conditional statements always return a true or a false.
Notice that we are switching 'true'. So we are saying , if ($i==6) is true, then proceed to the block and then break.
This evaluations in case statements along with the 'break;' and 'continue;' combinations make the PHP switch so powerful that it can completely replace the 'if...else' statements while keeping the code elegant !


