By Alvin Alexander. Last updated: June 4, 2016
Just a quick note here to share a Drupal get user id (uid) function. I've found that when I'm developing a Drupal module or Drupal application, I often need to access the user id, so I created this simple function and include it in a common utilities module:
# return the current drupal user id (uid) function get_user_id() { global $user; return $user->uid; }
As you can see, the code accesses the global Drupal user object ($user), then gets the uid from the user object.
I use this function all the time when performing SQL database queries where I need to limit data access to the current user, or add new data belonging to the current user, and occasionally use it for other purposes.