3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function binary_search(array $array, integer $target) { $max = count($array) - 2; $min = 0; while ($max - $min > 1) { if ($target < $array[($max - $min) / 2]) { $max = ($max + $min) / 2; } else { $min = ($max + $min) / 2; } } return $result = $array[$max + $min / 2] === $target ? true : false; } echo('Searching for 6 in 1,2,3,4,5,6'); $array = [1,2,3,4,5,6]; $target = 6; $result = binary_search($array, $target); echo($result); echo('Searching for 3 in 1,2,3,4,5'); $array = [1,2,3,4,5]; $target = 3; $result = binary_search($array, $target); echo($result);
based on SpcKV
Output for 7.3.0alpha1 - 7.3.0rc3
Searching for 6 in 1,2,3,4,5,6 Fatal error: Uncaught TypeError: Argument 2 passed to binary_search() must be an instance of integer, int given, called in /in/fa0VD on line 20 and defined in /in/fa0VD:3 Stack trace: #0 /in/fa0VD(20): binary_search(Array, 6) #1 {main} thrown in /in/fa0VD on line 3
Process exited with code 255.
Output for 7.0.28 - 7.2.11
Searching for 6 in 1,2,3,4,5,6 Fatal error: Uncaught TypeError: Argument 2 passed to binary_search() must be an instance of integer, integer given, called in /in/fa0VD on line 20 and defined in /in/fa0VD:3 Stack trace: #0 /in/fa0VD(20): binary_search(Array, 6) #1 {main} thrown in /in/fa0VD on line 3
Process exited with code 255.
Output for 5.6.38
Searching for 6 in 1,2,3,4,5,6 Catchable fatal error: Argument 2 passed to binary_search() must be an instance of integer, integer given, called in /in/fa0VD on line 20 and defined in /in/fa0VD on line 3
Process exited with code 255.