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.

6
  • 1
    ($b && $c && $d && $e) returns a boolean (true or false), not a numeric value. Commented Jan 28, 2018 at 17:41
  • 5
    switch(max($a,$b,$c...)) {case $a: ... break; case $b: ... break; ...} Commented Jan 28, 2018 at 17:41
  • What do you think that the && operator does? That is, what should the result of 1 && 2 be? Commented Jan 28, 2018 at 17:42
  • return i put there just because this is am creating a shortcode for wordpress plugin Commented Jan 28, 2018 at 17:43
  • 1
    @Fat-Bee That makes sense in a human language, but not here. If you do it like that, you need to write it out completely like $a > $b && $a > $c && $a > $d and so on. Commented Jan 28, 2018 at 17:50

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

1 Comment

How about some explanation here? Code is good, but documentation is better. +1 for good use of compact()
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.

2 Comments

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
Please provide values of a,b,c and the code you use. You can edit your question with that info.
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>";

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.