- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# PHP program to find standard deviation of values within an array

To find standard deviation of values within an array, the code is as follows in PHP −

## Example

<?php function std_deviation($my_arr) { $no_element = count($my_arr); $var = 0.0; $avg = array_sum($my_arr)/$no_element; foreach($my_arr as $i) { $var += pow(($i - $avg), 2); } return (float)sqrt($var/$no_element); } $my_arr = array(67, 89, 93, 102, 4); echo "The standard deviation of elements in the array is "; print_r(std_deviation($my_arr)); ?>

## Output

The standard deviation of elements in the array is 35.423156268181

A function named ‘std_deviation’ is defined counts the number of elements in the array and initializes the variance to 0. The average is calculated as the sum of elements in the array divided by the total number of elements in the array. Now, a ‘foreach’ loop is run over the array and the variance is calculated by subtracted the average value from every element of the array and squaring it.

When the foreach loop goes to the end, the final variance value is returned as output. Outside that function, an array is defined and that function is called on this array. Relevant output is displayed on the console.

- Related Questions & Answers
- C++ Program to Calculate Standard Deviation
- Java Program to Calculate Standard Deviation
- C program to calculate the standard deviation
- C++ program to implement standard deviation of grouped data
- How to find the moving standard deviation in an R matrix?
- How to find the moving standard deviation in an R data frame?
- Variance and Standard Deviation
- What is Standard Deviation of Return?
- How to find the standard deviation for rows in an R data frame?
- Find average of each array within an array JavaScript
- Print the standard deviation of Pandas series
- Average Returns and Standard Deviation of Securities
- Return the standard deviation of the masked array elements in NumPy
- Find average of each array within an array in JavaScript
- Sorting or Arranging an Array with standard array values - JavaScript

Advertisements