Passing Arrays to Function in PHP

Q

Can You Pass an Array into a Function? in PHP?

✍: FYIcenter.com

A

You can pass an array into a function in the same as a normal variable. No special syntax needed. Here is a PHP script on how to pass an array to a function:

<?php
function average($array) {
  $sum = array_sum($array);
  $count = count($array);
  return $sum/$count;
}
$numbers = array(5, 7, 6, 2, 1, 3, 4, 2);
print("Average: ".average($numbers)."\n");
?>

This script will print:

Average: 3.75

 

Creating Your Own Functions in PHP

⇒⇒PHP Tutorials

2016-12-18, 258👍, 0💬