• PHP Video Tutorials

PHP - Stats Harmonic Mean() Function



Definition and Usage

The stats_harmonic_mean() function can return the harmonic mean of an array of values. The harmonic mean is a type of numerical average. It is calculated by dividing the number of observations by the reciprocal of each number in the series. Thus, the harmonic mean is the reciprocal of the arithmetic mean of the reciprocals.

Syntax

  number stats_harmonic_mean( array $a )

Parameters

Sr.No Parameter Description
1

a

The input array

Return Values

The stats_harmonic_mean() function can return the harmonic mean of values in a or false if a is empty or is not an array.

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 harmonic mean of an array.

<?php
   var_dump(sprintf("%2.9f", stats_harmonic_mean(array(1,3,5,7))));
   }
?>

Output

This will produce following result −

string(11) "2.386363636"

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_harmonic_mean(): The array has zero elements

bool(false)
Advertisements