Stryves Posted May 7, 2009 Share Posted May 7, 2009 I'm not sure if this is bad practise or not, but is it possible to do this: <?php if(x=='1' && y=='1' || x=='2' && y=='5') { // Do This } Instead of <?php if(x=='1' && y=='1') { // Do This } else if(x=='2' && y=='5') { // Do This } I'd prefer the first method so that I am not duplicating code. I'm noticing that it looks like the code checks "|| x=='2'" and ignore's the && so that if x=='2' it runs, and ignores whatever y is equal to. Is that intended? Worst case scenario I'll just use included files to try and avoid reproducing a lot of code. Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/ Share on other sites More sharing options...
Stryves Posted May 7, 2009 Author Share Posted May 7, 2009 I'll try a test run using brackets like: ((x=='1' && y=='1') || (x=='2' && y=='5')) and see if this helps... lol. Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828720 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 It's not bad practice. In fact, you may need both cases depending on what code you want executed. And you would want to put parentheses there. Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828724 Share on other sites More sharing options...
Zhadus Posted May 7, 2009 Share Posted May 7, 2009 You need to use the parenthesis, but it will be fine then. This code: if(x=='1' && y=='1' || x=='2' && y=='5') is equivalent to: if(((x=='1') && (y=='1') && (y=='5')) || (x=='2')) Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828727 Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 ((x=='1' && y=='1') || (x=='2' && y=='5')) That is how it should be done if that is what you want. Way to solve your own question Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828731 Share on other sites More sharing options...
GingerRobot Posted May 7, 2009 Share Posted May 7, 2009 Stryves: Assuming the "Do This" code is the same, they're logically equivalent. However, the first is obviously nicer as it means you don't have to repeat that code. The if statement your first presented is indeed valid - the && operator has a higher precedence than the || operator, so is evaluated first. See this manual page for more on precedence. All that said, i personally would prefer to add the extra brackets as it makes it easier to read and you or other people are less likely to mistake the purpose of the if statement. E.g.: if( ($x=='1' && $y=='1') || ($x=='2' && $y=='5') ) Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828733 Share on other sites More sharing options...
Stryves Posted May 7, 2009 Author Share Posted May 7, 2009 I knew this was the right place to come... Just seconds after posted and trying to ensure I didn't sound like an idiot it clicked... "Oh right, brackets!" Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828740 Share on other sites More sharing options...
GingerRobot Posted May 7, 2009 Share Posted May 7, 2009 Like I said though, in the above case the brackets aren't actually necessary - I would still use them, however. Quote Link to comment https://forums.phpfreaks.com/topic/157260-solved-if-statement-using-or-instead-of-else-if-with-and-involved/#findComment-828742 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.