If the user is logged on then $USER->id should work - but if you are writing your own code, you may have to declare the $USER variable as a global in your code.
General developer forum
How to get user id of current user in moodle?
This discussion has been locked so you can no longer reply to it.