- You put function definitions within the
if
block which might be OK but is not very clean. If you need to call the functions from outside theif
block and the if condition is false, the functions will be undefined (you will get the fatal error). So put the function definitions outside theif
block, maybe at the end of the code. - Errors are in both functions. They are not syntactical errors but more semantical errors. The generateOrderedArray() is expected to return an array but in your case it returns a resource type. In the binarySearch function you address an element of an array
$element = $array[$mid]
but$element
is not an array.
This is how I think the generateOrderedArray should look like:
function generateOrderedArray()
{
$res = mysql_query("Select location from houserent");
$row = mysql_fetch_array($res);
print_r($row);
return $row;
}
In the binarySearch function check for the type first
while ($low <= $high)
{
$mid = floor(($low + $high) / 2);
if(isset($element) && !empty($element)) {
$element = $array[$mid];
...
} else {
return false;
}