Reputation: 127
Use of ||
(or)
I got so many values which I need to compare with same variable, is there a better way to write more efficiently such as $city == -35 || -34 || -33
or even simpler so that I don't have to repeat variable name since it's the same variable only value keep changing.
<?php
if ($city == -35 || $city == -34 || $xcity == -33)
{
$region = "noindex";
}
?>
Any suggestions?
Upvotes: 3
Views: 7705
Reputation: 270609
You might use in_array()
if (in_array($city, array(-35, -34, -33))) {
$region = "noindex";
}
Or if they are consecutive ( I suspec they aren't and this is just an example)
in_array($city, range(-35, -33))
Upvotes: 6
Reputation: 26189
Use associative array. in_array compare each element in array with your var. Associative array used hashtable. It faster.
$cities = array(-33 => 1, -34 => 1, -35 => 1);
if (isset($cities[$city])) {
$region = "noindex";
}
Upvotes: 0
Reputation: 57774
Assuming that $xcity
is a typo,
switch ($city)
{
case -35:
case -34:
case -33:
$region = "noindex";
break;
}
Upvotes: 1