inArray instead of multiple conditionals or switch case lines: () && () && () && ()

Tired of writing


$var != null && $var != undefined &&

forever…?


if( $var != 'NULL' && $var != ' ' && $var != undefined &&....... )

?

or:


switch($var){
case null: break;
case " ": break;
case undefined: break;
...
default:
}

?

Do this instead!


if (!in_array($var, array("", null, "undefined", 'etc.'))){
//
}

Example 2:


$var1 = 50, $var2 = 43, $var4 = 48;
$inProductArray = array($var1, $var2, $var3, $var4);

Instead of:


foreach($inProductArray as $temp){
switch($temp){
case 40:
case 44:
case 48:
case 52: echo "Flight + Hotel"; break;
case 41:
case 45:
case 49:
case 53: echo "Flight"; break;
case 42:
case 46:
case 50:
case 54: echo "Hotel"; break;
case 43:
case 47:
case 51:
case 55: echo = "Car"; break;
}
}// Result HotelCarFlight + Hotel

Do this!


foreach($inProductArray as $temp){
switch($temp){
case in_array($temp, array(40,44,48,52)): echo "Flight + Hotel"; break;
case in_array($temp, array(41,45,49,53)): echo "Flight"; break;
case in_array($temp, array(42,46,50,54)): echo "Hotel"; break;
case in_array($temp, array(43,47,51,55)): echo "Car"; break;
}
}// Result HotelCarFlight + Hotel

Leave a Reply

You must be logged in to post a comment.