You want to add all the values in an array.
Use array_sum()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $scores = array(4, 8, 12, 14, 16); print_r($scores); echo "<p><b>Use array_sum() to add the values</b></p>"; echo "Average score = ", array_sum($scores) / count($scores); ?> </p> </body> </html> |