Passing an array into a function with PHP

This tutorial explains how to pass an array as an argument in a function with PHP. The process of passing an array into a function is very much the same as passing a variable into a function. Watch the video below and then scroll down to see the sample code.

Sample PHP code:

<?php
  // create the 'scores' array
  $scores = array(9,7,112,89,633,309);

  // create the 'average' function
  function average($array){
    // set 'total' to 0
    $total = 0;
    foreach($array as $value){
      // adds the value of each item in the array, one by one
      $total += $value;
    }
    // calculate the average and return the result
    return $total/count($array);
  }

  // call the 'average' function and use the 'scores' array as argument
  echo "Average = ", average($scores);
?>