how can if statement allow me to assign a new variable at comparison at this example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th May 2021, 6:16 AM
Mustafa Emad
Mustafa Emad - avatar
6 Answers
+ 4
$var == 5 is true. It means 1. So, $num = 1, because == operator has higher precedence than =.
29th May 2021, 6:38 AM
Simba
Simba - avatar
+ 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"; }
29th May 2021, 8:11 AM
Simba
Simba - avatar
+ 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;
29th May 2021, 8:43 AM
Simba
Simba - avatar
+ 1
Simba oh i got it thank you
29th May 2021, 9:00 AM
Mustafa Emad
Mustafa Emad - avatar
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
29th May 2021, 6:59 AM
Mustafa Emad
Mustafa Emad - avatar
0
Simba it means that at two examples it made an assignment then if statement ckeck the value of the variable
29th May 2021, 8:20 AM
Mustafa Emad
Mustafa Emad - avatar