shane18 Posted December 19, 2009 Share Posted December 19, 2009 $A = 12; $B = 53; $C = 66; if($A = 3 && $B = 6 && $C = 9){ echo "$A,$B,$C<br>"; } After this runs, A = 1/B = 1/C=9 ...... why? $A = 12; $B = 53; $C = 66; if($A = 3 || $B = 6 || $C = 9){ echo "$A,$B,$C<br>"; } echo "$A,$B,$C"; After this runs, A = 12/B = 53/C=66 ...... why? Can someone explain this behavior to me? Quote Link to comment https://forums.phpfreaks.com/topic/185681-if-statement/ Share on other sites More sharing options...
Lamez Posted December 19, 2009 Share Posted December 19, 2009 in your if statements change it from an '=' to '=='. The if statement is returning false, and your last echo is showing the variables untouched. Quote Link to comment https://forums.phpfreaks.com/topic/185681-if-statement/#findComment-980465 Share on other sites More sharing options...
premiso Posted December 19, 2009 Share Posted December 19, 2009 But to answer your first question about 1,1,9 doing a var_dump on $A and $B you will see it is now a boolean, so basically they are being assigned the value of true, since $A = 1 was set is what I imagine and since it was a AND. I am not 100% sure why, but I am sure it is how assignments are done with the AND keyword, because all 3 statements must be true. The second part, is the same deal but this one is using OR and since the assignment is generally always done and is true, it sets the first variable to true and the rest do not get executed cause it is an OR statement and exits at the first true condition. I used the following code for testing, to come to these results: $A = 12; $B = 53; $C = 66; if($A = 3 || $B = 6 || $C = 9){ var_dump($A); var_dump($B); var_dump($C); echo "$A,$B,$C<br>"; } $A = 12; $B = 53; $C = 66; if($A = 3 && $B = 6 && $C = 9){ var_dump($A); var_dump($B); var_dump($C); echo "$A,$B,$C<br>"; } $A = "one"; $B = "two"; $C = "three"; if($A = 3 || $B = 6 || $C = 9){ var_dump($A); var_dump($B); var_dump($C); echo "$A,$B,$C<br>"; } //echo "$A,$B,$C<br />"; $A = "one"; $B = "two"; $C = "three"; if($A = 3 && $B = 6 && $C = 9){ var_dump($A); var_dump($B); var_dump($C); echo "$A,$B,$C<br>"; } echo "<br /><b>Lowercase Var Names</b><br/>"; $a = 12; $b = 53; $c = 66; if($a = 3 || $b = 6 || $c = 9){ var_dump($a); var_dump($b); var_dump($c); echo "$a,$b,$c<br>"; } //echo "$a,$b,$c<br />"; $a = 12; $b = 53; $c = 66; if($a = 3 && $b = 6 && $c = 9){ var_dump($a); var_dump($b); var_dump($c); echo "$a,$b,$c<br>"; } $a = "one"; $b = "two"; $c = "three"; if($a = 3 || $b = 6 || $c = 9){ var_dump($a); var_dump($b); var_dump($c); echo "$a,$b,$c<br>"; } //echo "$a,$b,$c<br />"; $a = "one"; $b = "two"; $c = "three"; if($a = 3 && $b = 6 && $c = 9){ var_dump($a); var_dump($b); var_dump($c); echo "$a,$b,$c<br>"; } But yea, I am not sure what is really up, as $A should be 3 by all logic...but yea, that was my findings, perhaps someone knows why, as I do not fully know why just a glimpse in my mind =\ Quote Link to comment https://forums.phpfreaks.com/topic/185681-if-statement/#findComment-980467 Share on other sites More sharing options...
shane18 Posted December 19, 2009 Author Share Posted December 19, 2009 Thanks for all the replies, but does anyone know why the if statement changes the VALUES of the variables in it... except for the last one.... im trying to understand this behavior. Quote Link to comment https://forums.phpfreaks.com/topic/185681-if-statement/#findComment-980582 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.