- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- 3
- Output for 7.4.0 - 7.4.33
- Parse error: syntax error, unexpected '|', expecting ';' or '{' in /in/v0WrH on line 8
Process exited with code 255.
<?php
class MyMath
{
/**
* @throws Exception
*/
public function calculateMedian(array $array): int|float
{
if (!$array) {
throw new LengthException('Cannot calculate median because Argument #1 ($array) is empty');
}
sort($array);
$middleIndex = count($array) / 2;
if (is_float($middleIndex)) {
return $array[(int) $middleIndex];
}
return ($array[$middleIndex] + $array[$middleIndex - 1]) / 2;
}
}
$math = new MyMath;
var_export($math->calculateMedian([1, 2, 3, 4, 5]));