dk4210 Posted May 6, 2011 Share Posted May 6, 2011 Hello guys, I have a question about the logic of OR (||) and && I have a form and on the form is asks for hair color <div class="hcenter"><div class="box3">Hair color</div></div> <div class="box2"><input type="radio" name="hair" value="Black" />Black<br /> <input type="radio" name="hair" value="Brunette" />Brunette<br /> <input type="radio" name="hair" value="Blonde" />Blonde<br /> <input type="radio" name="hair" value="Red" />Red<br /></div> I grab the post var like this $hair = $_POST['hair']; // I actually have it filtered but you get the point I created this function function check_Haircolor($hair){ if (isset($hair) && $hair != 'Brunette' || $hair != 'Black' || $hair != 'Red' || $hair != 'Blonde' ){ Do something here } } But it would not work correctly.. I kept doing the "Do something here...." so I tried the && and it worked (Se below) as expected. I would have bet that the or (||) would have been the correct code.. function check_Haircolor($hair){ if (isset($hair) && $hair != 'Brunette' && $hair != 'Black' && $hair != 'Red' && $hair != 'Blonde' ){ Do something here } } Why did the && word and not the || Thanks, Dan Link to comment https://forums.phpfreaks.com/topic/235705-logic-question-with-and/ Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Erm.. isset($hair) will always be true Link to comment https://forums.phpfreaks.com/topic/235705-logic-question-with-and/#findComment-1211470 Share on other sites More sharing options...
Drummin Posted May 6, 2011 Share Posted May 6, 2011 I think because you're using NOT != Plus the isset is only being applied to the first variable isset($hair) && $hair != 'Brunette' Link to comment https://forums.phpfreaks.com/topic/235705-logic-question-with-and/#findComment-1211473 Share on other sites More sharing options...
Adam Posted May 6, 2011 Share Posted May 6, 2011 if (isset($hair) && $hair != 'Brunette' || $hair != 'Black' || $hair != 'Red' || $hair != 'Blonde' ){ In English this would be; if $hair is set (as MadTechie says, will always be true anyway) and $hair does not equal Brunette, or if $hair does not equal black, or if $hair does not equal red, or if $hair does not equal blonde, ... Even excluding the isset check, that statement would always evaluate to true. Link to comment https://forums.phpfreaks.com/topic/235705-logic-question-with-and/#findComment-1211478 Share on other sites More sharing options...
dk4210 Posted May 6, 2011 Author Share Posted May 6, 2011 Thanks for the response guys.. I think I understand it now... It was throwing me for a loop on that one.. Link to comment https://forums.phpfreaks.com/topic/235705-logic-question-with-and/#findComment-1211495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.