How do I put extra number at the end of username if duplicate

前端 未结 8 1888
醉话见心
醉话见心 2021-01-19 23:31

Hard to explain, and let me show an example.

  1. If username foo already exists in MySQL I want php script will allow it but to be foo1
8条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 00:24

        while (exists($username)) {
            if (!is_numeric(substr($username, -1, 1))) {
                $username = "{$username}0";
            }
            $username++;
        }
    
        return $username;
    

    You get: test, test1, test2 and so on

提交回复
热议问题