Thanks @absiddiqueLive, one question what about if {$username}-{$countUser} was also present in db, since its only checking once and appending a number at the end of username?
You could also consider a slightly different approach.
Why not use the email as unique identifier for a user? That way you have an easy (and verifyable way) to determine if a user is unique. In that case you don't have to make changes to the first and lastname either.
Well in that case, does the profile-page have to public?
If not, then:
example.com\profile
should be sufficient.
If the profilepage should be publicly accessable then why not explicitly ask the user for a username. That gives the user the freedom to take whatever nickname he wants, and gives you the advantage of easy validating a name against a column in your database.
If you want to make it even prettier, have an ajax-request validate the username realtime against the database. Using vue.js this isn't that hard to write.
@sid405 Yes I do have an username field in users table, but i am not asking user to enter username in signup form, I want to generate unique username automatically based on first/last name , later user can change it if they wants by visiting account setting
@saqueib Although i really don't see the point of letting them choose, you can generate a unique id concatenating 'Bob' with the hashid https://github.com/vinkla/hashids of the user->id.
But like @zachleigh said you'll end up with 10 users whose username will start with bob and then 10 digits of gibberish.
Really don't see a point in this, unless there's a part of the application logic you're not mentioning.