How to create user in drupal programmatically?

    The code under can be used to create new user. We can also add profile fields values if we are using profile module there. Also extra role can be assigned to the user by mentioning role id and the name of role in the roles array
    = 'mohit';
    $password = '****';
    $email = '';
    $firstname = 'Mohit'; //Only if profile is used
    $lastname = 'Sharma'//Only if profile is used
    $siteuser = array(
    'name' => $name,
    'mail' => $email,
    'status' => 1,
    'pass' => $password,
    'roles' => array(DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    4 => 'Customer'), //Extra Role
    'profile_firstname' => $firstname, //Profile fields If we are using
    'profile_lastname' => $lastname//Profile fields If we are using
    $newuser = user_save('', $siteuser);

    //Login after user is created
    $myaccount = user_authenticate($siteuser);
    Average: 5 (1 vote)