shane18 Posted December 21, 2009 Share Posted December 21, 2009 <? $A = 2; $B = 5; if($A = 22 && $B = 54){ echo $A . "-" . $B . "<br>"; echo "PASS"; }else{ echo $A . "-" . $B . "<br>"; echo "FAIL"; } ?> Why does it convert the $A to its Boolean value... but leaves $B alone? it always leaves the last one in the statement alone.... anyone know why? Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/ Share on other sites More sharing options...
oni-kun Posted December 21, 2009 Share Posted December 21, 2009 You are assigning variables in the IF, Not comparing them. if($A = 22 && $B = 54){ Should be if($A == 22 && $B == 54){ And just to note, you should not use short tags (i.e. <?), you should only used <?php Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981250 Share on other sites More sharing options...
shane18 Posted December 21, 2009 Author Share Posted December 21, 2009 im saying if u do something like... if($A = blah() && $B = blah2()){ lets say i wanted 2 run two functions and store there results in the if instead of having more code... why don't it work? Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981253 Share on other sites More sharing options...
trq Posted December 21, 2009 Share Posted December 21, 2009 Because as noted = is an assignment operator, not a comparison operator. Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981254 Share on other sites More sharing options...
oni-kun Posted December 21, 2009 Share Posted December 21, 2009 I told you that you are assigning not comparing. If you wanted to use functions in an IF. look: <?php $a = 1; $b = 2; function foo(){ return 1; } function bar(){ return 2; } if ($a == foo() && $b == bar()) { echo "It works!" ; } ?> And it should work as expected. See how I used ==? Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981256 Share on other sites More sharing options...
shane18 Posted December 21, 2009 Author Share Posted December 21, 2009 im trying to assign a variable... and have it checked... i know that to compare A with B u go A == B... i wana be able to assign a function to a variable... then have it checked to be TRUE or FALSE like if($TEST = BLAH()){ be sides.... why does it convert every variable except for the last one... ? it'll return like 1 1 1 1 64... if i assign 5 variables in the if statement Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981257 Share on other sites More sharing options...
oni-kun Posted December 21, 2009 Share Posted December 21, 2009 im trying to assign a variable... and have it checked... i know that to compare A with B u go A == B... i wana be able to assign a function to a variable... then have it checked to be TRUE or FALSE like if($TEST = BLAH()){ be sides.... why does it convert every variable except for the last one... ? it'll return like 1 1 1 1 64... if i assign 5 variables in the if statement What on earth are you trying to do? if($A = 22 && $B = 54){ echo $A . "-" . $B . "<br>"; This will always be true. You can do if ($a == foo()) { $a = pass; } An IF statement is not for assigning. Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981260 Share on other sites More sharing options...
shane18 Posted December 21, 2009 Author Share Posted December 21, 2009 Well, i just wondering why if you do.... if($A = A() && $B = B()) Lets say... each function can return 0-2 A returns 2... and b returns 2... for example... why after the if statement does A = 1 and B = 2..... why does it only convert the first variable to its boolean value Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981261 Share on other sites More sharing options...
shane18 Posted December 21, 2009 Author Share Posted December 21, 2009 ur right what im trying to do is pointless i just wana kno why php... only converts all but the last assigned variable Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981262 Share on other sites More sharing options...
trq Posted December 21, 2009 Share Posted December 21, 2009 Its not converting anything to a boolean value. if after you execute your example $A equals one then 1 is what is being returned by the A function. Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981264 Share on other sites More sharing options...
shane18 Posted December 21, 2009 Author Share Posted December 21, 2009 lol it don't even matter... i was just wondering... Link to comment https://forums.phpfreaks.com/topic/185830-if-statement-behavior/#findComment-981267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.