preg_grep() returns an array of values that match the given pattern. If the username is valid, there should be no match, and preg_grep() should return an empty array. (which will be treated as "false").
So the exclamation mark shouldn't be there. It'll force the script to do the exact opposite as it's supposed to.
if (preg_grep(...)) means "If the given username matches one (or more) string(s) in the
bad names array".
If you want to run the code only when you actually POST a username, wrap it into this: