₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,000 members, 8,419,871 topics. Date: Thursday, 04 June 2026 at 05:25 AM

Toggle theme

Help With This Php Code. - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHelp With This Php Code. (1740 Views)

1 Reply (Go Down)

Help With This Php Code. by okeyxyz(op): 5:41am On Jan 25, 2012
Hi guys, I need help with this php code. i'm trying this simple binary-search code to confirm whether an input number is present in an ordered array, but i'm not getting any feedback from it so far. it's supposed to reply TRUE or FALSE. i donno what's wrong. thx.

<?php 

//sample array
$MyArray =array(12, 13, 17, 21, 24, 35, 37, 42, 49, 50, 53, 60);

//Define search function
function confirm_value(& $Array, $Value){
$Value1 =$Value;        //value to check for
$High =count($Array) -1;                //last array index
$Low =0;        //first array index
$Index;        //selected index


while($Low <= $High){
$Index =(int)($High + $Low)/2;

if ($Array[$Index] ==$Value1){
return TRUE;
}
else if ($Array[$Index] <$Value1){
$High =$Index -1;
}
else $High =$Index +1;

}
return FALSE;
}

//try search function
echo confirm_value($MyArray, 49);


sorry guys, i tried posting with the site code button but the fonts r too small. so i'm reposting with the quote button.

<?php
//sample array
$MyArray =array(12, 13, 17, 21, 24, 35, 37, 42, 49, 50, 53, 60);

//Define search function
function confirm_value(& $Array, $Value){
$Value1 =$Value;       //value to check for
$High =count($Array) -1;               //last array index
$Low =0;       //first array index
$Index;       //selected index


while($Low <= $High){
$Index =(int)($High + $Low)/2;

if ($Array[$Index] ==$Value1){
return TRUE;
}
else if ($Array[$Index] <$Value1){
$High =$Index -1;
}
else $High =$Index +1;

}
return FALSE;
}

//try search function
echo confirm_value($MyArray, 49);
Re: Help With This Php Code. by Nobody:
Re: Help With This Php Code. by okeyxyz(op): 9:17am On Jan 25, 2012
thanx man!! i see my mistake now. My search was moving away from $Value1 instead of towards it. cheers.
Re: Help With This Php Code. by dellnet: 10:03am On Jan 25, 2012
Ah arrays, every student's nightmare. I will advice you when dealing with arrays to avoid interfering with the index for example $High = count($Array) - 1;

-1 can lead to logic errors later in your program.
Re: Help With This Php Code. by blackrules1(m): 8:08am On Feb 02, 2012
@ Omo t' o dun.

Kindly advice me on how to get a C++ compiler. I need it pls.
Re: Help With This Php Code. by Nobody: 6:54am On Feb 05, 2012
Ah well, there be many compilers, but i kinda prefer "codeblocks" - u might want to look it up in google.
But wait, i think alhaji omo_to_dun uses a different compiler o.
1 Reply

Pls Help Me With This Php CodeHelp On Php Code For A Dice GameI Need Help With This Php Code For Email And Sms Autoresponse234

Create A Beautiful Android Weather App Using Openweathermap APIHow Do I Convert Finger Print Image To Binary Stream?..help PlsA Tech Startup Programmer