To write a program to find Standard Deviation of an array we are going to use below PHP inbuilt functions.
- array_sum(): returns the sum of all the elements of an array.
- count(): gives the number of elements currently present in the given array.
- sqrt(): returns the square root of the given number.
To find the standard deviation, we must have to first calculate the variance. Variance can be calculated as the sum of the squares of difference between all the numbers and the means.Finally we will use the formula to get the standard deviation is :-
√(variance/number_of_elements)
PHP program to find the Standard Deviation of an array:-
<?php // Write user defined Standard_Deviation function to calculate the standard deviation function Standard_Deviation($array) { $number_of_elements = count($array); $variance = 0.0; // using array_sum() function to calculate mean $avg = array_sum($array)/$number_of_elements; foreach($arrayas$i) { $variance += pow(($i - $avg), 2); } return (float)sqrt($variance/$number_of_elements); } // Take Input array as below $array = array(7,6,9,11,27); print_r(Standard_Deviation($array)); ?>
Output:-
