I have the following recursive function that won't return anything if it has to loop once. I can echo the username in the return condition and it shows the correct username after the loop, with an appended number (e.g. jdoe_45), but it doesn't return it.
function createUsername ($conn, $firstname, $lastname, $numerate = false) {
$fn = preg_replace("/[^A-Za-z0-9 ]/", '', $firstname);
$ln = preg_replace("/[^A-Za-z0-9 ]/", '', $lastname);
$arg = ($numerate) ? '_' . rand(11, 99) : '';
$username = strtolower(substr($fn, 0, 1) . $ln . $arg);
$sql = "SELECT * FROM users WHERE username = '$username'";
$rs = mysql_query($sql, $conn) or die ("Could not execute query.");
$num = mysql_numrows($rs);
if ($num == 0) {
return $username;
}
createUsername($conn, $firstname, $lastname, 1);
}