Jump to content

If Statement


shane18

Recommended Posts

$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?

Link to comment
https://forums.phpfreaks.com/topic/185681-if-statement/
Share on other sites

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 =\

Link to comment
https://forums.phpfreaks.com/topic/185681-if-statement/#findComment-980467
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.