Question:
I need to do a comparison between three variables in my PHP code.
if($a == $b == $c) {
return true;
} else {
return false;
}
I have a lot of ideas on how to do this, but I want to know the best way to achieve this result.
Answer:
The simplest, most direct and objective solution is
return ($a === $b && $a === $c);
in case you just want to compare values, and not the types, you can use like this:
return ($a == $b && $a == $c);
in the latter case, using ==
instead of ===
One comment:
In the question you used this if:
if($a == $b == $c) {
return true;
} else {
return false;
}
When you use a logical condition, like ($a == $b)
, the result is either true or false . In such a case you can simply return the result with return ( $a==$b )
, as the if is totally redundant and unnecessary, since the comparison is already the desired answer.