Search code examples
phpoperatorssimplification

Simplifying greater than IF statements with multiple ANDs based on the same value


I have a variable that I need to check against other variables to make sure it is greater than them. I've simplified the example below, but here's a working example of the desired result.

<?php
  $a = 5;
  $b = 4;
  $c = 3;
  $d = 2;
  $e = 1;

  if(($a > $b) && ($a > $c) && ($a > $d) && ($a > $e)){
    echo "A is the biggest";
  }else{
    echo "A is not the biggest";
  }
?>

My question is, is there a simpler way to write the if statement so that we don't have to have $a written four separate times? Something along the lines of ...

if($a > $b,$c,$d,$e){

I've seen Simpler way to check if variable is not equal to multiple string values? but this is for checking the presence of strings.


Solution

  • I'd write something like that:

    if (max($a, $b, $c, $d, $e) == $a) {
        ....
    }