3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function binary_search(array $array, int $target) { $max = count($array) - 1; $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 8UMhC
Output for 7.0.28 - 7.3.0rc3
Searching for 6 in 1,2,3,4,5,6 Notice: Undefined offset: 7 in /in/EXt1H on line 14 Searching for 3 in 1,2,3,4,5 Notice: Undefined offset: 5 in /in/EXt1H on line 14
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 int, integer given, called in /in/EXt1H on line 20 and defined in /in/EXt1H on line 3
Process exited with code 255.