formula455 Posted June 10, 2008 Share Posted June 10, 2008 Hi.. if i remember right this code i'm about to show used to work. I don't know if i'm doing something wrong... or if my webserver is just not supporting it. <? if($var == (("condition1") || ("condition2"))) { echo "fubar"; } ?> basically if $var is either condition1 or condition2 it would echo fubar. what's happening is on my webserver it's echoing fubar reguardless of what either condition is. if i make each condition seperate it works.. like so.... <? if($var == "condition1" OR $var == "condition2") { echo "fubar"; } ?> i could technically do it this way... if theres going to be alot of conditions and i would prefer to use the first route. so yah if theres something i'm doing wrong please gimmi the heads up otherwise i guess its my webserver not allowing the conditional commands Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/ Share on other sites More sharing options...
Lefteris Posted June 10, 2008 Share Posted June 10, 2008 Hey mate. I am no expert but in the first statement I think you are telling php to check : if($var == condition1) or if condition2 is true. It is not comparing var to condition 2. And I guess condition 2 will always be true since it is not getting compared to something , so the whole expression will always be true so .. it will always echo"fubar"; Ofcourse I might misunderstand something but I hope I helped. Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/#findComment-561875 Share on other sites More sharing options...
RMcLeod Posted June 10, 2008 Share Posted June 10, 2008 ^^^ Yep that's basically it so change your code to <?php if($var == "condition1"|| $var == "condition2") { echo "fubar"; } ?> Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/#findComment-561878 Share on other sites More sharing options...
formula455 Posted June 10, 2008 Author Share Posted June 10, 2008 i figured that's what I would have to do =/ so theres no way of getting around repeatingly typing the variable $var? Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/#findComment-561883 Share on other sites More sharing options...
Lefteris Posted June 10, 2008 Share Posted June 10, 2008 well you could use a switch statement if you got too many checks to make: switch($var) { case condition1: case condition2: case condition3: case condition4: ..................... case conditionn: echo"fubar"; break; default: echo"not fubar "; break; } Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/#findComment-561889 Share on other sites More sharing options...
trq Posted June 10, 2008 Share Posted June 10, 2008 Or an array. <?php $conditions = array('foo','bar','bob'); if (in_array($var,$conditions)) { echo '$var matches a condition'; } ?> Link to comment https://forums.phpfreaks.com/topic/109533-conditional-commands/#findComment-561907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.