get the average of a number list using an array in php

get the average of a array of numbers

PHP

$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

PHP

$rating_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$average_rating = array_sum($rating_array) / count($rating_array);
if($average_rating > 0) { // if the average rating is greater than 0
  $average_rating = number_format($average_rating,2); // format it to 2 decimal places. so you dont get something like 1.2222223
} else {
  $average_rating = 0;
}
$template_result = "The average rating of array(1, 2, 3, 4, 5, 6, 7, 8, 9) is " . $average_rating;

The average rating of array(1, 2, 3, 4, 5, 6, 7, 8, 9) is 5.00

Other Items in php
Page Views

This page has been viewed 281 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
I believe that success can be measured in the number of uncomfortable conversations you're willing to have.
Unknown