PHP Adicionar número ao nome existente

$name = 'foo';
$first_name = $name;
$i = 0;
do {
  //Check in the database here
  $exists = exists_in_database($name);
  if($exists) {
    $i++;
    $name = $first_name . $i;
  }
}while($exists);
//save $name
Indian Gooner