mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
27 lines
580 B
PHP
27 lines
580 B
PHP
<?php
|
|
|
|
/**
|
|
* This function calculates
|
|
* The median value of
|
|
* numbers provided
|
|
*
|
|
* @param decimal $numbers A variable sized number input
|
|
* @return decimal $median Median of provided numbers
|
|
* @throws \Exception
|
|
*/
|
|
function median(...$numbers)
|
|
{
|
|
if (empty($numbers)) {
|
|
throw new \Exception('Please pass values to find mean value');
|
|
}
|
|
|
|
sort($numbers);
|
|
$length = count($numbers);
|
|
$middle = ceil($length / 2);
|
|
if ($length % 2 == 0) {
|
|
return ($numbers[$middle] + $numbers[$middle - 1]) / 2;
|
|
}
|
|
|
|
return $numbers[$middle - 1];
|
|
}
|