- array_slice: documentation ( source)
- range: documentation ( source)
<?php
function BinSearch($needle, $haystack){
$midpoint = count($haystack) / 2;
$lower_half = array_slice($haystack, 0, $midpoint);
$upper_half = array_slice($haystack, -$midpoint, count($haystack));
if($needle < $midpoint){
BinSearch($needle, $lower_half);
} else if($needle > $midpoint){
BinSearch($needle, $upper_half);
}
if(count($haystack == 1)){ return true;} else if(count($haystack) == 0) { return false;}
}
$x = range(0,999);
BinSearch(82,$x);