arrays - Foreach loop and average in PHP -
i'm rather new programming in general. i'm starting off exercises i'm kind of getting stuck. created array , looped thru foreach loop print out each individual number stored in array, dont know find average of numbers , print out.
<?php $myarray = array(87,75,93,95); foreach($myarray $value){ echo "$value <br>"; } ?>
you could:
$avg = array_sum($myarray) / count($myarray); echo $avg;
where:
array_sum
calculates sum of elements in given array.count
outputs total number of elements in given array.
Comments
Post a Comment