WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
There's a speed difference in single & double quotes!? – JakeParis Jan 28 '11 at 14:51
+1 for the part about the variable disappearing. Good thought! – JakeParis Jan 28 '11 at 14:52
@JMC Creative: There can be a (very, very small) difference. The PHP parser generates different output for double quoted strings that contain variables vs. single quoted strings that you concatenate yourself. The difference is measured in milliseconds, when you have a large amount of string operations. So nothing to worry about. (In Backie's comparison I personally think the function vs. global variable is slower than single vs. double quoted strings, but in both cases it doesn't matter. The argument about forward compatibility is much more important.) – Jan Fabry Jan 28 '11 at 15:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.