<?php
function bsearch(array $array, int $target): int {
$min = 0;
$max = count($array) - 1;
while ($max >= $min) {
$mid = floor(($min + $max) / 2);
if ($target == $array[$mid]) {
return $mid;
}
if ($target > $array[$mid]) {
$min = $mid + 1;
} else {
$max = $mid -1;
}
}
return -1;
}
$array = [1, 2, 3, 4, 5];
for ($i = 0; $i < 7; $i++) {
echo bsearch($array, $i) . PHP_EOL;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: