3v4l.org

run code in 300+ PHP versions simultaneously
<?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]));
Output for git.master, git.master_jit, rfc.property-hooks
3

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
29.89 ms | 401 KiB | 8 Q