Custom Function: Update user role based on membership

Hi there,

I'm looking for a custom function that allows to update the user's role based on their membership.

$new_role = 'premium_role';
// update user role using wordpress function
function getMembership($ID) {
$membership['ID']=getUserMeta($ID, 'membership');
if($membership['ID']!==premium''){
wp_update_user( array ('ID' => $user_id, 'role' => $new_role ) ) ;}
//add_role('premium_role');

So when a user subscribe for a new membership, his role is automatically updated

Thank you.

Regards,

Mark