Why does under initial conditions deduce that $ a is max?

$b=400; $c=300; $a=350; $d = ($b > $c && $b > $a) ? true : false; $e = ($a > $c && $a > $b) ? true : false; $f = ($c > $a && $c > $b) ? true : false; if ($e=true){ echo "$a is max"; } elseif ($d=true){ echo "$b is max"; } else { echo "$c is max"; } 
  • Because you have if ($e=true) instead of if ($e == true) (or shorter if ($e) ) - BOPOH
  • 2
    Fundamentals of the language: the sign = indicates the assignment, the sign == (and also === ) - test for equality. - VladD
  • I think you should refresh those PHP books you read. - VladD
  • Thank you, I just started to learn all this) - Dima

1 answer 1

You have the wrong "equal." It is necessary so:

 if ($e==true){ echo "$a is max"; } elseif ($d==true){ echo "$b is max"; } else { echo "$c is max"; } 

Or even so, for any variable containing true-false:

 if ($e){ echo "$a is max"; } elseif ($d){ echo "$b is max"; } else { echo "$c is max"; } 

Better yet, call variables meaningfully. Well, a moment of theory:

A single sign = is an assignment, a double == is a comparison without considering the type of a variable (for example, the integer 1 is a fractional number 1.0), and the triple === takes into account the type.

PS Noticed how your condition successfully optimized your code? The variable $ f in it was not useful :)

  • Thank you) Already figured out) - Dima