A means of comparing three variables

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 this, but I want to know how best to achieve this.

    
asked by anonymous 11.02.2014 / 19:32

4 answers

20

The simplest, direct and objective solution is

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

If you just want to compare values, not types, you can use 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, such as ($a == $b) , the result is already true or false . In such a case you can simply return the result with return ( $a==$b ) , since if is totally redundant and unnecessary, since the comparison is already the desired answer.

    
11.02.2014 / 20:00
7

I think the most efficient is:

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

Remembering that == tests whether variables have equivalent values (for example, 0 is equal to null ) and === tests whether the variables are of the same type E have the same value

    
11.02.2014 / 19:34
6

This is how you did it, but you have to test one thing at a time and use the equivalent operator of 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
}

In addition to the fact that there is a difference between the operator equal == , which says that the values are equal, and the identical operator === that besides having the equal value, the type is the same, there is one more very important thing in the order of the operators:

  

When using the && operator, always put the least costly condition first, because if it fails the second and will not be tested

And another very important thing

  

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

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

    
11.02.2014 / 19:42
2

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

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

So if you need to check each of the comparisons separately then it's a lot easier.

    
11.02.2014 / 19:51