dongmu5106 2018-01-28 17:39
浏览 113

在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.

  • 写回答

5条回答 默认 最新

  • douzhenyu6533 2018-01-28 17:51
    关注

    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
    
    评论

报告相同问题?