function BSearch($x, $t) { if (!is_array($x)) return -1; $n = sizeof($x); $l = -1; $u = $n; $p = 0; $m = 0; while ($l + 1 != $u) { $m = ceil(($l + $u) / 2); if ($x[$m] < $t) { $l = $m; } else { $u = $m; } } $p = $u; if ($p >= $n || $x[$p] != $t) { $p = -1; } return $p;}