aebstract Posted March 13, 2009 Share Posted March 13, 2009 Don't know what to subject this, but what is the best way to do something like this: if ($variable == 1 || 2 || 3 || 4 ){ do something } I know that is incorrect, which is why I asked =[ Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/ Share on other sites More sharing options...
Silverado_NL Posted March 13, 2009 Share Posted March 13, 2009 should be able to do this $var = 1; if($var == 1 OR 2 OR 3 OR 4){ // do it } not tested though Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783978 Share on other sites More sharing options...
ionik Posted March 13, 2009 Share Posted March 13, 2009 it is not good practive to use use OR use || If this is just a range of numbers from 1-4 if ($var <= 4) { // Do Stuff Here } if you are refering to checking a variable aganist a few possibilites it is done as so if ($var == exp) || $var == expr || $var == expr) { // Do as you which } Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783982 Share on other sites More sharing options...
aebstract Posted March 13, 2009 Author Share Posted March 13, 2009 Okay I should have explained it a little better, it's not going to be numbers. It'll be a few set things. so it could be: abc h3j 33k aa0. I'm guessing just put in an array and use in_array though? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783985 Share on other sites More sharing options...
samshel Posted March 13, 2009 Share Posted March 13, 2009 should be able to do this $var = 1; if($var == 1 OR 2 OR 3 OR 4){ // do it } not tested though i think this will not work... try this.. $var = 1; if($var == 1 OR $var == 2 OR $var == 3 OR $var == 4){ // do it } OR better <?php $var = 1; $arr = array(1,2,3,4); if(in_array($var,$arr)){ echo "Sucess"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783987 Share on other sites More sharing options...
aebstract Posted March 13, 2009 Author Share Posted March 13, 2009 Yeah going with option 2, was hoping to stay away from option 1. Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783989 Share on other sites More sharing options...
ionik Posted March 13, 2009 Share Posted March 13, 2009 check my updated post and read this best tutorial http://us.php.net/manual/en/language.expressions.php Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783992 Share on other sites More sharing options...
Silverado_NL Posted March 13, 2009 Share Posted March 13, 2009 it is not good practive to use use OR use || why not? Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783996 Share on other sites More sharing options...
samshel Posted March 13, 2009 Share Posted March 13, 2009 its with regard to the situation..if the number of things are increasing and after some time you have say 100 items to check, then it is easier to define an array instead of writing 100 conditions with OR ofcourse this is my thought. Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-783998 Share on other sites More sharing options...
Silverado_NL Posted March 13, 2009 Share Posted March 13, 2009 ah yeah, i thought u saying there was some big difference between OR and || but yeah if ur checking against value's the array way is the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-784009 Share on other sites More sharing options...
ionik Posted March 13, 2009 Share Posted March 13, 2009 it is not good practive to use use OR use || why not? http://us3.php.net/manual/en/language.operators.logical.php they have no precedence over other logical operators Quote Link to comment https://forums.phpfreaks.com/topic/149287-easy-way-to-check-a-value/#findComment-784013 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.