Search code examples
phpvariablesif-statementswitch-statementminimum

stuck with getting two minimums of several variables


I'm pretty new to PHP, still learning, but I just cant get the solution.

I have 3 variables

$line1 = 5;
$line2 = 3;
$line3 = 7;

I need a hint on how to get the 2 lowest variables and IF the sum of those 2 variables is higher than the 3rd variable it should echo.

I'm guessing an if-else statement or switch, but im not sure.
But i'm mostly stuck on how to get the 2 minimums.

Anyone can help? Sorry, im still a noob xD


Solution

  • $all = [$line1, $line2, $line3];
    
    sort($all);
    
    if ($all[0] + $all[1] > $all[2]) {
        echo $all[2];
    }
    

    This should do.

    Next time you should atleast try before asking and show us what you have done beforehand. This is not a "Cant do it, can you do it for me please?" but rather "I have done this, can you help me fix it?" type.