PHP Tutorials - Math Functions in PHP, Math functions on Array in PHP


Math Functions in PHP, Math functions on Array in PHP


The PHP provides the several math-type functions which are used to perform the math operation on arrays. The array_sum() function is used to adds the vale of all stored elements of an array and returns that answer to a variable. The count() function is used to count the number of elements of an array returns the number of elements in a variable.
Here is a simple example to demonstrate these two functions
<?php

$marks = array(87,88,98,74,56,94,67) ;
$sum_of_marks = array_sum($marks);
echo "The sum of the provided Marks is: $sum_of_grades <br/>";
$avgMarks = array_sum($marks) / count($marks) ;
echo "and the average of these Marks is: " . round($avgMarks,2) ;
?>

Output
The sum of the provided Marks is: 564
The average of these Marks is: 80.57

I have used the round() function to round the value of average grades into two decimal places.

{ 0 comments... read them below or add one }

Post a Comment