Skip to content

Instantly share code, notes, and snippets.

@obojdi
Created August 2, 2017 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obojdi/cbb9d4a44b39920cc90af756d22606e8 to your computer and use it in GitHub Desktop.
Save obojdi/cbb9d4a44b39920cc90af756d22606e8 to your computer and use it in GitHub Desktop.
random username checker
<?php
$dbh = ['user_10', 'user_02', 'user_05', 'user_08', 'user_07', 'user_06', 'user_09'];
$a = function ($un = '') use ($dbh, &$a){
$random_digit = sprintf("%02d", rand(0, 10));
$username_random = "user_" . $random_digit;
if(strlen($un)){
$username = $un;
}else{
$username = $username_random;
}
if(in_array($username, $dbh)){
// username exists, check again
echo $username . ' exists ';
return $a();
}else{
// username does not exist, use it
echo ' use $username ';
return $username;
}
};
$b = $a();
echo $b;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment