Local Tech Repair: Range Switch Statements in PHP

Wednesday, February 29, 2012

Range Switch Statements in PHP

For those that are wondering how to use a switch statement to check the ranges of number in php here you go.

there are different ways of checking ranges you can either use a if statement. Though a if statement gets extremely blocky over many different checks.

I prefer using switches to check my ranges. Here is an example of how to do that.



function formid($start) {
$start = (int)$start;
switch ($start) {
case ($start < (int)3):
return 1;
break;
case ($start < (int)7):
return 2;
break;
case ($start > (int)7):
return 2;
break;
default:
return 0;
break;
}

$num = 3;
echo formid($num); // this will return 2




So you don't have to do it the hard way with all those if statements you can just use a switch.

I hope you liked this little article

Local Tech Repair Admin

No comments:

Post a Comment