<?php
//In the search function, $array is an array, $k is the value to be found, $low is the minimum key value of the search range, and $high is the maximum key value of the search range
function search($array, $k, $low=0, $high=0)
{
if(count($array)!=0 and $high == 0) //Determines if this is the first call
{
$high = count($array);
}
if($low <= $high) //If there are any remaining array elements
{
$mid = intval(($low+$high)/2); //Take the middle of $low and $high
if ($array[$mid] == $k) //Returns if found
{
return $mid;
}
elseif ($k < $array[$mid]) //If it is not found, continue the search
{
return search($array, $k, $low, $mid-1);
}
else
{
return search($array, $k, $mid+1, $high);
}
}
return -1;
}
$array = array(4,5,7,8,9,10); //Test the search function
echo search($array, 8); //Call the search function and output the search results
?>