NYCPHP Meetup

NYPHP.org

[nycphp-talk] Help with while loop

Ken Robinson kenrbnsn at rbnsn.com
Mon Nov 27 15:17:36 EST 2006


At 03:10 PM 11/27/2006, Randal Rust wrote:
>I have always had trouble with trying to write these from scratch. The
>code below is essentially what I have been working on, but it never
>stops looping. What am I missing here?
>
><?php
>
>$teams=array(
>'Reds',
>'Browns',
>'Bengals',
>'Cavaliers',
>'Blue Jackets',
>'Indians'
>);
>
>$unique=false;
>$newTeam=$_POST['newTeam'];
>
>function checkDuplicate($value){
>         global $teams;
>         $count=0;
>         //do query here, return an array
>         for($i=0; $i<count($teams); $i++){
>                 if($value==$teams[$i]){
>                         $count++;
>                         }
>                 }
>         if($count == 0){
>                 //no matches
>                 return true;
>                 }
>         else {
>                 //there are matches
>                 return false;
>                 }
>         }
>
>while(!$unique){
>         $check=checkDuplicate($newTeam);
>         if($check){ $unique=true; } else { $unique=false; }
>         }
>
>?>

You don't need a loop here. The loop is in the function, where it belongs.

All you need is:

$unique = checkDuplicate($newTeam);

Ken 




More information about the talk mailing list