How to create user programatically if not exist? I am using this code to create user. 

  if(user_validate_name('czxc') != ""){
        $user = User::create([
                'name' =&gt;'czxc',    
                'mail' =&gt; 'user@gmail.comgczxcdfa',
                'pass' =&gt; 'password',
                'status' =&gt; 1
             ]);
            $user-&gt;save();
            drupal_set_message($user-&gt;id());

    }else{
        drupal_set_message('Already exist user'); 
    }
   This code works only one time for me.
The user_validate_name function from Drupal only verifies the syntax of the given name.

It does not validate whether the username exists or not.

You could replace the function call, like so:

$uids = \Drupal::entityQuery('user')
  -&gt;condition('name', 'czxc')
  -&gt;execute();
if (count($uids) &gt; 0) {
  $user = User::create([
    'name' =&gt;'czxc',
    'mail' =&gt; 'user@gmail.comgczxcdfa',
    'pass' =&gt; 'password',
    'status' =&gt; 1
  ]);
  $user-&gt;save();
  drupal_set_message($user-&gt;id());

} else {
  drupal_set_message('Already exist user');
}