# If Statement

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

##### Share on other sites

in your if statements change it from an '=' to '=='.

The if statement is returning false, and your last echo is showing the variables untouched.

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

##### Share on other sites

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.

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.