wds Posted August 8, 2007 Share Posted August 8, 2007 when i test this code: <? $i = 0; while($i < 20){ if(($i / 5) == (1 || 2 || 3)){ print($i . " " . ($i / 5) . "<br>"); } $i++; } ?> It outputs: 1 0.2 2 0.4 3 0.6 4 0.8 5 1 6 1.2 7 1.4 8 1.6 9 1.8 10 2 11 2.2 12 2.4 13 2.6 14 2.8 15 3 16 3.2 17 3.4 18 3.6 19 3.8 Yet this code: <? $i = 0; while($i < 20){ if(($i / 5) == 1 || ($i / 5) == 2 || ($i / 5) == 3){ print($i . " " . ($i / 5) . "<br>"); } $i++; } ?> Gives me the correct output: 5 1 10 2 15 3 i don't understand why i can't use the previous code, not that it really matters, it just seems tedious to have to write all that for every time. any ideas why ($i / 5) == (1 || 2 || 3) does not work, yet ($i / 5) == 1 || ($i / 5) == 2 || ($i / 5) == 3 does work? Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/ Share on other sites More sharing options...
lemmin Posted August 8, 2007 Share Posted August 8, 2007 That is simply the syntax. The == operator can only support one value on either side). By the time it gets to the || operator, the comparisons have already been done and it is only separating the true or false values. so when you do if(($i / 5) == (1 || 2 || 3)), you are comparing $i/5 to (true or true or true). Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/#findComment-318902 Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 if(($i / 5) == (1 || 2 || 3)){ [/cdoe] i guess doing something like that will be the same as if(($i / 5) == bool){ (1 || 2 || 3) == boolean value witch is true or false so doing this if(($i / 5) == (1 || 2 || 3)){ returns true or false in this case i guess your getting true because 0 is false and non zero is true so you always get true why? ($i / 5) is not zero so its like saying true = true hope that helps Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/#findComment-318903 Share on other sites More sharing options...
clanstyles Posted August 8, 2007 Share Posted August 8, 2007 LoL U beat me lemmin i was going to say. Because its the rules of programing you just can't have TRUE == TRUE TRUE TRUE And teng Grrr.. Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/#findComment-318904 Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 LoL U beat me lemmin i was going to say. Because its the rules of programing you just can't have TRUE == TRUE TRUE TRUE And teng Grrr.. actually you can but its non sense Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/#findComment-318905 Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 i believe the problem is solved please mark this as solved Link to comment https://forums.phpfreaks.com/topic/63976-solved-confused-about-or-syntax/#findComment-318908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.