Web Programming with PHP

14 Array Exercises

<?php
$expenses = array(12, 45, 10, 25);
$total = 0;
for($i = 0; $i < count($expenses); $i++)
$total += $expenses[$i];
echo "Total $total\n";
echo "Average " . ($total / $i);
?>

<?php
$expenses = array(12, 45, 10, 25);
$total = 0;
foreach($expenses as $value)
$total += $value;
echo "Total $total\n";
echo "Average " . ($total / count($expenses);
?>