3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Searching for 6 in 1,2,3,4,5,6 Deprecated: Implicit conversion from float 2.5 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 1.25 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 0.625 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 7.1875 to int loses precision in /in/EXt1H on line 14 Warning: Undefined array key 7 in /in/EXt1H on line 14 Searching for 3 in 1,2,3,4,5 Deprecated: Implicit conversion from float 5.5 to int loses precision in /in/EXt1H on line 14 Warning: Undefined array key 5 in /in/EXt1H on line 14
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 Searching for 6 in 1,2,3,4,5,6 Deprecated: Implicit conversion from float 2.5 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 1.25 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 0.625 to int loses precision in /in/EXt1H on line 8 Deprecated: Implicit conversion from float 7.1875 to int loses precision in /in/EXt1H on line 14 Warning: Undefined array key 7 in /in/EXt1H on line 14 Searching for 3 in 1,2,3,4,5 Deprecated: Implicit conversion from float 5.5 to int loses precision in /in/EXt1H on line 14 Warning: Undefined array key 5 in /in/EXt1H on line 14
Output for 8.0.0 - 8.0.30
Searching for 6 in 1,2,3,4,5,6 Warning: Undefined array key 7 in /in/EXt1H on line 14 Searching for 3 in 1,2,3,4,5 Warning: Undefined array key 5 in /in/EXt1H on line 14
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
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 7.3.32 - 7.3.33
Searching for 6 in 1,2,3,4,5,6Searching for 3 in 1,2,3,4,5
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 int, integer given, called in /in/EXt1H on line 20 and defined in /in/EXt1H on line 3
Process exited with code 255.

preferences:
218.1 ms | 401 KiB | 286 Q