kolip kolip - 3 months ago 5
PHP Question

How do I get the highest value when using loops?

I want to get the highest value of my array.
This are the two ways when I'm working with php functions.


$a = array(1,125,1068);
1. $value = max($a);
print_r ($value);

2. asort($a);
$value = end($a);
print_r ($value);


I just couldn't figure out how to get the highest value when using loops.

Answer

You do it like this:

$highest = 0;
//if you have negative values: $highest = min($a);
foreach($a as $item){
    if ($item > $highest){
        $highest = $item;
    }
}
Comments