3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getNearestMatch($number) { $arr = array_values([2, 4,4, 8, 16,16, 23]); sort($arr); $key = 0; while(isset($arr[$key+1]) && $arr[$key] < $number) $key++; if($arr[$key] == $number || !isset($arr[$key-1]) || $arr[$key] - $number < $number - $arr[$key-1]) return $arr[$key]; else return $arr[$key-1]; } //To test it we pass some values to the function foreach([0, 2, 3, 4, 8, 12, 22, 23, 24] as $v) echo getNearestMatch($v) . PHP_EOL;
Output for 5.4.10 - 7.2.0
2 2 2 4 8 8 23 23 23
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/j0Frv on line 4
Process exited with code 255.