In the search function, $ array is the array, $ k is the value to be searched, $ low is the minimum key value in the search range, and $ high is the maximum key value in the search range.
The code is as follows:
// In the search function, $ array is the array, $ k is the value to be searched, $ 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 whether it is the first call.
{
$ High = count ($ array );
}
If ($ low <= $ high) // if there are still remaining array elements
{
$ Mid = intval ($ low + $ high)/2); // Obtain the center values of $ low and $ high.
If ($ array [$ mid] === k) // if it is found, return
{
Return $ mid;
}
Elseif ($ k <$ array [$ mid]) // if not found, continue searching
{
Return search ($ array, $ k, $ low, $ mid-1 );
}
Else
{
Return search ($ array, $ k, $ mid + 1, $ high );
}
}
Return-1;
}
$ Array = array (,); // test the search function
Echo search ($ array, 8); // call the search function and output the search result.
?>