only as an exercise, becuse if you actully wanted to do this you would use @kittykittybangbang's answer
<?php
$myArray = array(87,75,93,95);
$sum='';//create our variable
foreach($myArray as $value){
$sum+=$value; //adds $value to $sum
//echo "$value <br>";
}
echo $sum;
?>
for the count count($myArray);
makes the most senses but you could do that in the loop as well:
<?php
$myArray = array(87,75,93,95);
$sum= $count=0;// initiate interger variables
foreach($myArray as $value){
$sum+=$value; //adds $value to $sum
$count++; //add 1 on every loop
}
echo $sum;
echo $count;
//the basic math for any average:
echo $sum/$count;
?>
if you don't create $sum
and $count
before the loop you would get notices returned from php, as the first time it tried to add to either, there would be noting to add to
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…