PHP - Stats Variance() Function



Definition and Usage

The stats_variance() function can return the variance.

Syntax

  float stats_variance( 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_variance() function can return the variance of values in a, or false on failure.

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 the variance of values in a.

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

Output

This will produce following result −

float(3.6)
float(4.5)
Advertisements