PHP or operator ||
Use ||
(or)
I have so many values that I need to compare with the same variable, is there a better way to write more efficiently, for example, $city == -35 || -34 || -33
or even simpler, so that I don't have to repeat the name of the variable, since it has the same variable, only the value changes ...
<?php
if ($city == -35 || $city == -34 || $xcity == -33)
{
$region = "noindex";
}
?>
Any suggestions?
+3
source to share
5 answers
you can use in_array()
if (in_array($city, array(-35, -34, -33))) {
$region = "noindex";
}
Or, if they are consistent (I suspect they are not, and this is just an example)
in_array($city, range(-35, -33))
+6
source to share