mcfmullen Posted July 1, 2010 Share Posted July 1, 2010 <?php if (trim($table) == 'pigs' || 'sheep' || 'goats' || 'cows' || 'flamingoes'){ echo "blah blah blah"; } else { echo "bleep bleep bleep"; ?> The code above doesn't work. It echoes blah blah blah no matter what $table is set to. How am I supposed to use OR during an If statement? Link to comment https://forums.phpfreaks.com/topic/206436-if-or/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2010 Share Posted July 1, 2010 That's because trim($table) == 'pigs' is evaluated first, then the result of that is OR'd with 'sheep' and the result of that is OR'd with 'goats' ... The result of which is always TRUE. You would need to write out each comparison or since you are comparing one variable with a list of values, use the in_array function. Link to comment https://forums.phpfreaks.com/topic/206436-if-or/#findComment-1079894 Share on other sites More sharing options...
AbraCadaver Posted July 1, 2010 Share Posted July 1, 2010 $table = trim($table); if ($table == 'pigs' || $table == 'sheep' || $table == 'goats' || $table == 'cows' || $table == 'flamingoes') { echo "blah blah blah"; } else { echo "bleep bleep bleep"; } Or you can do something like: $animals = array('pigs', 'sheep', 'goats', 'cows', 'flamingoes'); if(in_array(trim($table), $animals)) { echo "blah blah blah"; } else { echo "bleep bleep bleep"; } Link to comment https://forums.phpfreaks.com/topic/206436-if-or/#findComment-1079896 Share on other sites More sharing options...
kenrbnsn Posted July 1, 2010 Share Posted July 1, 2010 That's not how you write the condition, use <?php if (trim($table) == 'pigs' || trim($table) == 'sheep' || trim($table) == 'goats' || trim($table) == 'cows' || trim($table) == 'flamingoes'){ ?> or you can use a switch statement: <?php switch (trim($table)) { case 'pigs': case 'sheep': case 'goats': case 'cows': case 'flamingoes': echo 'blah blah blah blah'; break; default: echo 'bleep bleep bleep'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/206436-if-or/#findComment-1079897 Share on other sites More sharing options...
mcfmullen Posted July 1, 2010 Author Share Posted July 1, 2010 Thank you very much everyone! Link to comment https://forums.phpfreaks.com/topic/206436-if-or/#findComment-1079899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.