Fat-Bee
Fat-Bee

Reputation: 73

Compare variable to multi variables in PHP

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.

Upvotes: 3

Views: 438

Answers (5)

Amr Berag
Amr Berag

Reputation: 1110

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.

Upvotes: 0

David Newcomb
David Newcomb

Reputation: 10943

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;
    ...
}

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

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>";

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

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.

Upvotes: 2

splash58
splash58

Reputation: 26153

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

Upvotes: 3

Related Questions