Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP or statement ||

Tags:

php

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?

like image 981
Mas Avatar asked Dec 04 '25 22:12

Mas


2 Answers

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))
like image 101
Michael Berkowski Avatar answered Dec 07 '25 13:12

Michael Berkowski


Yes. Use in_array,

in_array($city, array(-35,-34,-33))
like image 42
mpen Avatar answered Dec 07 '25 15:12

mpen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!