3

I have 5 variables $a $b $c $d $e . These variables has numerical values. Im trying to compare these variables where each variable will be compared to the rest and if the condition is true it echoes something. Here is my code

if ($a > ($b && $c && $d && $e)) {
$result = '<div>Im A</div>'
} else if ($b > ($a && $c && $d && $e)) {
$result = '<div>Im B</div>'
} else if ($c > ($a && $b && $d && $e)) {
$result = '<div>Im C</div>'
} else if ($d > ($a && $b && $c && $e)) {
$result = '<div>Im D</div>'
} else if ($e > ($a && $b && $c && $d)) {
$result = '<div>Im E</div>'
}

return $result;

The result stops at first condition even though it is false and it should pass it to other conditions.

Fat-Bee
  • 73
  • 1
  • 8

5 Answers5

3

Some different approach:

$a = 1;
$b = 3;
$c = 4;
$d = 5;
$e = 0;

// Make array [a=>1, b=>3...] 
$arr = compact('a','b','c','d','e');
// Sort it in descending order with saving keys
arsort($arr);
// Get the 1st key
echo 'I\'m ' . strtoupper(key($arr)); // I'm D
splash58
  • 26,043
  • 3
  • 22
  • 34
2

First of all - you have parentheses around ($b && $c && $d && $e), this means that in $a > ($b && $c && $d && $e) the result of ($b && $c && $d && $e) will be counted first, and then will be compared to $a.

So, $a > ($b && $c && $d && $e) is not

$a is greater than $b and $a is greater than $c and etc.

it is

$a is greater than result of ($b and $c and $d and $e)

And result of $b and $c and $d and $e is either true or false.

So, in the end you compare $a > true or $a > false. According to value of $a you can get different results.

In a simple case, if you want to check if something is greater than anything else you need to write a condition like:

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

Other more tricky solutions you will find in other users' anwers.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • good explanation, the problem i have now is if i only use `>` it doesnt return anything, when i try `>=` it runs the first condition and stops there even though it is false and i want it to run to the true statment – Fat-Bee Jan 28 '18 at 18:05
  • Please provide values of a,b,c and the code you use. You can __edit__ your question with that info. – u_mulder Jan 28 '18 at 18:06
0

You can iterate your five variables and keep track of the one with the highest value.

for ($i='a', $x = 0, $max = 'x'; $i <= 'e'; $i++) {
    if ($$i > $$max) {
        $max = $i;
    }
}
$result = "<div>I'm " . strtoupper($max) . "</div>";
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

I think you should steer clear of if statements ;) Here's a solution without them that can be used with any number of variables. You could use the $key as an index into a list of functions and make the program extensible.

// Make the numbers into an array
$o = [$a, $b, $c, $d, $e];
// Find the highest value
$max = max($o);
// Look up the highest value to get the index
$key = array_search($max, $o);
// Now you know which one it is, you can do anything with it!
switch ($key) {
    case 0: $result = '<div>Im A</div>'; break;
    case 1: $result = '<div>Im B</div>'; break;
    ...
}
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
0

Try this:

$array = ['A'=>$a,'B'=>$b,' C'=>$c,'D'=>$d,'E'=> $e];
$maxs = array_keys($array, max($array));
foreach ($maxs as $maxi ) 
  echo '<div>Im '.$maxi.'</div>."<br>"';

This way it covers cases were there are multiple max values.

Refer to this answer for more about how it works.

USER249
  • 1,080
  • 7
  • 14