3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: "integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning in /in/fa0VD on line 3 Searching for 6 in 1,2,3,4,5,6 Fatal error: Uncaught TypeError: binary_search(): Argument #2 ($target) must be of type 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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: "integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning in /in/fa0VD on line 3 Searching for 6 in 1,2,3,4,5,6 Fatal error: Uncaught TypeError: binary_search(): Argument #2 ($target) must be of type 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.3.0 - 7.3.33, 7.4.0 - 7.4.33
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.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
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.0 - 5.6.40
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.

preferences:
207.83 ms | 401 KiB | 287 Q