Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active September 5, 2019 04:23
Show Gist options
  • Save raazon/9b54a5b37d91f443d69b947ea858340d to your computer and use it in GitHub Desktop.
Save raazon/9b54a5b37d91f443d69b947ea858340d to your computer and use it in GitHub Desktop.
Do while loop if username exists in php
<?php
// custom function for test only. You don't need to use this function if you use it with wordpress.
// Wordpress have already this function: https://developer.wordpress.org/reference/functions/username_exists/
function username_exists($user_login){
$names = ['Razon', 'Razon-1', 'Razon-2', "Supu"];
if(in_array($user_login, $names)){
return $user_login;
}else{
return false;
}
}
$name = "Razon";
$baseName = $name;
$i = 0;
while(username_exists($name)) {
$name = $baseName .'-'. (++$i);
}
return $name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment