function binsearch($x,$a){

    $c=count($a);

    $lower=0;

    $high=$c-1;

    while($lower<=$high){

        $middle=intval(($lower+$high)/2);

        if($a[$middle]>$x){

            $high=$middle-1;

        } elseif($a[$middle]<$x){

            $lower=$middle+1;

        } else{

            return $middle;

        }

    }

    return false;

}