Share on Google+Share on Google+

php array binary search

This section demonstrates how to use the binary search Array in the php

  • PHP Binary Search Array is used to search the given value in the array.
  • In php there is no function for the binary search like java or other language.
  • User can implement and use the binary search in php as given below





     function binarySearch($value,$first,$last) {

         if($last<$first)   {

              return -1;



          global $ar;


          if($a===$value)  {

              return $mid;


          else {

              if($a>$value) {



         else {

              if($a<$value) {





       return binarySearch($value,$first,$last);


     foreach ($ar as $a)

     echo " ".$a;


     if($y>=0)  {

     echo "<br>".$val." is present at ".$y." position in the given array";


     else  {

     echo "<br>".$val ." is not prsesnt in the given array ";




Posted on: October 28, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null