peps666 Posted September 27, 2007 Share Posted September 27, 2007 First post on this forum! I'm currently developping a web program at my job. In some scripts, i have fairly long list of conditions before showing something. Here's a short example: if(($a==2 OR $a==4 OR $a==31)) { echo "this"; } I'm trying to save some space in the code to make it more readable. I tried to write it like this but it's not working... if(($a==(2 OR 4 OR 31)) { echo "$a"; } I tried this also... if(($a==(2 || 4 || 31)) { echo "$a"; } Is there a way to write correctly a statement in the way it is above? Thanks! Link to comment https://forums.phpfreaks.com/topic/70846-solved-fairly-simple-syntax-question/ Share on other sites More sharing options...
cooldude832 Posted September 27, 2007 Share Posted September 27, 2007 first off The word OR is a mysql operator not php secondly you can say if($a is this or that or that) you must say if($s is this or $a is that or $a is that) such as if($a = 5 || $a = 6 || $a = that holds true if $a is 5 6 or 8 also you can't test if 5<$a<8 you must say if($a>5 && $a < to make stuff clearer try using a function based test, or try this <?php $tester = array(5,6, $a = 2; if(in_array($a,$tester)){ //it matched } ?> make sense? Link to comment https://forums.phpfreaks.com/topic/70846-solved-fairly-simple-syntax-question/#findComment-356158 Share on other sites More sharing options...
peps666 Posted September 27, 2007 Author Share Posted September 27, 2007 Yep excellent. I thought there was some way to simply write if $a==(2 || 4 || It seems not. That's alright. The idea of the array in the function is good. Thank you. Case solved. Link to comment https://forums.phpfreaks.com/topic/70846-solved-fairly-simple-syntax-question/#findComment-356162 Share on other sites More sharing options...
trq Posted September 27, 2007 Share Posted September 27, 2007 first off The word OR is a mysql operator not php Not true. if(($a==2 OR $a==4 OR $a==31)) { echo "this"; } is perfectly valid php. Link to comment https://forums.phpfreaks.com/topic/70846-solved-fairly-simple-syntax-question/#findComment-356164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.