Way to make a comparison between three variables

Asked

Viewed 4,295 times

10

I need to make 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 that, but I want to know the best way to achieve that result.

  • 3

    What is the "best way"? The fastest? The most readable?

4 answers

21


The simplest, most direct and objective solution is

return ($a === $b && $a === $c);

if you just want to compare values, and not types, you can use so:

return ($a == $b && $a == $c);

in the latter case, using == instead of ===

A comment:

On 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 already true or false. In such a case you can simply return the result with return ( $a==$b ), because if is totally redundant and unnecessary, since the comparison is already the desired answer.

7

I think the most efficient is:

if ($a === $b && $a === $c) {
    return true;
}
return false;

Remembering that == test whether the variables have equivalent values (for example, a 0 amounts to one null) and the === test if the variables are of the same type E have the same value.

6

It’s like you did, but you have to test one thing at a time and use the operator equivalent to E/AND.

// Nota: bloco de código alterado. Espero que da próxima vez os colegas
//       estejam mais preocupados com o foco da questão e comentários
//       relacionados a performance e redução de erros do que ficar preso
//       estilo de código, em especial quando for apenas uma cópia do código
//       original e não tenha sido solicitado um regractoring completo
if ($a === $b && $b === $c) {
   // Bloco de código
} else {
  // Outro bloco de código
}

Besides the fact that there is difference between the equal operator ==, that the values are equal, and the identical operator === which besides having the same value, the type is the same, has something else very important in the order of operators:

When using the operator &&, always put the least expensive condition first, because if it fails the second nor will it be tested

And another very important thing

Handle the order of operations with && because it will make a difference if one operation can only occur if the previous one occurred before, or if one of the operations will generate error that the other is not true.

The two information in the yellow boxes are often forgotten or only noticed late, when the developer realizes that there has been some error message in the system.

  • Who gave -1 can explain the reason? What is wrong in my answer?

  • I kept it because the author’s question was about the conditionals testing equality on more than two operators, not on Return, and I explained much more important differences to the focus of the question they involve performance and reduction of errors in implementation. Next time you are going to deny someone else’s answer to a question you also answered, unless someone has already complained and you give a +1 in the person’s comment, comment why you gave -1. This behavior is aggressive. Keep in mind when thinking about it again with others.

  • The answer is good +1. I believe that if there was already confusion of the AP when using $a == $b == $c, then it was important yes explain about the operator’s use &.

2

The easiest way to do this is by separating the comparisons, so you will be able to know exactly what is giving true or false. Example:

<?php
if(($a == $b) && ($a == $c)) {
    return true;
} else {
    return false;
}
?>

That way if you need to check each of the comparisons separately then it’s much easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.