For custom modules and well-defined roles, we sometimes want to check to see if a user is in the a particular role.
The easiest way to do so is to check the user's roles array:
global $user;
if (!empty($user->uid)) {
if (in_array('desiredRole', $user->roles)) {
// user is in role, perform desired actions here
}
}
The $user object is in the global name space, so using the global declaration in a function is necessary.
$user->uid value will be 0. empty returns true for 0/null/empty values, as explained on php.net. in_array checks the desiredRole is in the user's roles.