Friday, August 28, 2015

how to get max number from an array

/*
* How to get max number from a array
* */
$numbers = array(9, 0, 1, 77, 4, 5, -1, 10);

$max_number = $numbers[0];
$length = count($numbers);//in order to save time we initial length outside of the loops
for($i = 0; $i < $length; $i++){
   if ($max_number < $numbers[$i]){
       $max_number = $numbers[$i];
   }
}
echo "Max number is the array is ", $max_number;

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }