$user_id vs. is_user_logged_in()

I found a code scrap on the internet which uses

if($user_id) {

instead of

if ( is_user_logged_in() ) {

to check if the user is logged in. I would assume that the first would be slightly faster because it’s not running a function, but can anyone verify that this would always work?

Solutions Collecting From Web of "$user_id vs. is_user_logged_in()"

Well it wouldn’t always work unless you global $user_id. is_user_logged_in will however work without that extra line of code. The speed improvement is most likely so small it’s less than the speed improvement between single and double quotes and not even worth thinking about.

Also $user_id variable may disappear in a new version and would promptly break your code, were as is_user_logged_in will be about for ages even if they decide to deprecate it.