0
how can if statement allow me to assign a new variable at comparison at this example?
in this example if statement accept variable declaration but another example like if ( $var = 5) { echo $var; } it will give true every time https://code.sololearn.com/wIMyK6IxzYnD/?ref=app
6 Respuestas
+ 4
$var == 5 is true. It means 1.
So, $num = 1, because == operator has higher precedence than =.
+ 4
$var = 5 is a true statement.
I mean the condition $var = 5 is evaluated. When it is true, the statements inside the if are executed and the program outputs its value.
Try this to understand it clearly
if ( $var = 0 ) {
echo $var;
} else {
echo "False";
}
+ 4
In the most of the programming languages, everything is evaluated to true except zero.
If the condition is true its returns the first Statement else another.
Hope you know the value of the true is one whereas false is zero.
Here is the another example for you
$var = 5
if ( 0 ) {
echo $var - 3;
} else {
echo $var + 3;
+ 1
Simba oh i got it thank you
0
Simba i mean why it accept variable declaration at this example i tried before as assignment but it forced condition to be true every time
0
Simba it means that at two examples it made an assignment then if statement ckeck the value of the variable