How to simplify PHP logical operator? -
i have searched , read documentation , experimented on these logical operator seem nothing works. want simplify if statement if variable same don't need keep rewriting it. example:
<?php if($var_1 == 'val_1' || $var_1 == 'val_2' || $var_1 == 'val_3' || $var_2 == 'val_4' || $var_2 == 'val_2' || $var_3 == 'val_5') { // } else { .... } ?>
i want simplify this:
<?php if($var_1 == ('val_1' || 'val_2' || 'val_3') || $var_2 == ('val_4' || 'val_2') || $var_3 == 'val_5') { // something.. } ?>
however code above not work, rather writing $var_1 on , on each different value, how write once ? strictly looking answers using if statement, not switch or other statement, know can use switch case, looking logical operator.
thank in advance.
you can use in_array()
function. more on subject: http://php.net/manual/en/function.in-array.php . e.g.
<?php //define arrays or them query.. etc $array1 = array("val_1", "val_2", "val_3"); $array2 = array("val_4, val_2"); $array5 = array("val_5"); //check if variables in defined arrays if (in_array("var_1", $array1) || in_array("var_2", $array2) || in_array("var_3", $array3)) { //do } ?>
Comments
Post a Comment