Last active
September 5, 2019 04:23
-
-
Save raazon/9b54a5b37d91f443d69b947ea858340d to your computer and use it in GitHub Desktop.
Do while loop if username exists in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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