farsighted Posted July 22, 2008 Share Posted July 22, 2008 Hey, I'm really new to php, and coding in general, so please forgive me if anything I say is dumb. What I'm trying to do is this: I have a variable ($variable) that I want to compare to the instances of an array ($values) using an if/else statement like this: if ($variable == $values) { code to be executed What I need to know is, how do I compare the variable to all the instances of the array? Would I have to use a loop function? If so, I'd need to be pointed in the right direction... Or can I just do something like: if ($variable == $values[0, 1 ,2]) { //I know that doesn't work, but is there some sort of way like that to do it? Is there a way of saying if ($variable == $values[0] or $values[0] or $values[0]) { code to be executed Thanks Shane Link to comment https://forums.phpfreaks.com/topic/116010-array-question/ Share on other sites More sharing options...
samshel Posted July 22, 2008 Share Posted July 22, 2008 you can use function in_array(), no need to loop thru array if you just want to compare to all elements <?php $arr = array("a", "b", "c"); $variable = "a"; if(in_array($variable, $arr)) { ..code to be executed } ?> Link to comment https://forums.phpfreaks.com/topic/116010-array-question/#findComment-596482 Share on other sites More sharing options...
farsighted Posted July 22, 2008 Author Share Posted July 22, 2008 Thanks for the quick reply! I'll give that a try... Link to comment https://forums.phpfreaks.com/topic/116010-array-question/#findComment-596489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.