How to programmatically assign user roles I am assigning a role to user by this code:

$user = user_load($user-&gt;uid);
$role = user_role_load_by_name("10-dis");
$user-&gt;roles = $user-&gt;roles + array($role-&gt;rid =&gt; $role-&gt;name);
user_save($user);


I need to replace this role with a new role, means I need to change user role according to some condition. How can I programmatically reassign user role?
Drupal core user.module provides a simple function to add/remove roles to/from a user. See user_multiple_role_edit().

$uid = 12345; // The user ID of the account you want to change.
$role_id = 4; // The numeric role ID you want to add.
$role_id_rm = 3; // The numeric role ID you want to remove.

user_multiple_role_edit(array($uid), 'add_role', $role_id);
user_multiple_role_edit(array($uid), 'remove_role', $role_id_rm);