Recursive PHP function is not returning expected value


I am trying to create a function that will create a unique username for each user stored in the database.

My plan was to create a username by concatenating the first and last name then check if this username was taken. If it wasn't just store it in the database and if it was then add a number on the end of it.

For example if ConnorAtherton was taken the function would next check ConnorAtherton1, ConnorAtherton2 until it found a unique username.

Here is the function (I have added some echo statements for debugging)

    function createUserName($username, $counter){

        global $fname, $lname;

        echo "\t\t\tUsername at Start - " . $username . "\n";

        // connect to database
        require($_SERVER['DOCUMENT_ROOT'] . "/inc/db.connect.php");

        $stmt = $conn->prepare('SELECT * FROM users WHERE username = ?');
        $stmt->bind_param('s', $username); 

        echo "\t\t\tUsername before loop - " . $username . "\n";

            if( $stmt->num_rows > 0){

                //construct original name and try again
                $username = ucfirst($fname) . ucfirst($lname) . $counter;
                createUserName($username, $counter);


            echo "\t\t\tUsername after loop - " . $username . "\n\n";

            return $username;

Here is what it returns to the console

    Username at Start - ConnorAtherton
    Username before loop - ConnorAtherton
    Username at Start - ConnorAtherton1
    Username before loop - ConnorAtherton1
    Username at Start - ConnorAtherton2
    Username before loop - ConnorAtherton2
    Username after loop - ConnorAtherton2

    Username after loop - ConnorAtherton1

It returns the correct value after the loop (ConnorAtherton2) to begin with but I don't know why there would be a second value after the loop.

It returns ConnorAtherton1 and I need it to return ConnorAtherton2.

Any help is greatly appreciated.




The code executes as expected. What you are seeing is just the stack unwinding from the recursive calls.

You should modify your function to exit after the recursive call, by returning the result from the recursive call

    return createUserName($username, $counter);
 //... Rest of fn omitted



You can achieve what you want without recursion much easier. The basic logic would look like this:

$counter = '';
do {
    $username = ucfirst($fname) . ucfirst($lname) . ($counter++);
} while(!doesNameExist($username));

You'd only have to implement thedoesNameExist() method. There you do the DB query and returntrue if the name already exists.

The($counter++) part will append an empty string on first iteration (the++ suffix increases the variable after the value was evaluated).

People are also looking for solutions to the problem: php - Joomla, How to disable the “Flash Uploader”


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.