• PHP Video Tutorials

PHP - Stats Standard Deviation() Function



Definition and Usage

The stats_standard_deviation() function can return the standard deviation.

Syntax

  float stats_standard_deviation( array $a [, bool $sample = false ] )

Parameters

Sr.No Parameter Description
1

a

The array of data to find the standard deviation for. Note that all values of the array will be cast to float.

2

sample

Indicates if a represents a sample of the population; defaults to false.

Return Values

The stats_standard_deviation() function can return the standard deviation of the values in "a" on success, or false on failure.

The stats_standard_deviation() function can raise "E_WARNING" when there are fewer than 2 values in a.

Dependencies

This function was first introduced in statistics extension (PHP 4.0.0 and PEAR 1.4.0). We have used latest release of stats-2.0.3 (PHP 7.0.0 or newer and PEAR 1.4.0 or newer) for this tutorial

Example

In the following example, we compute standard deviation of the values in an array.

<?php
   $a=array(4, 1, 7);
   $dev=stats_standard_deviation($a);
   var_dump(sprintf("%2.9f", $dev));
?>

Output

This will produce following result −

string(11) "2.449489743"

Example

In the following example, we compute standard deviation of the values in an array by passing $sample=1.

<?php
   $a=array(5,7,8,10,10);
   var_dump(stats_standard_deviation($a,1));
?>

Output

This will produce following result −

float(2.1213203435596)

Example

Following is an error case. In the following example, we pass empty array. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_standard_deviation(array()));
?>

Output

This will produce following result and a warning in logs PHP Warning:stats_standard_deviation(): The array has zero elements

bool(false)
Advertisements