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?

1 Answer 1


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.

  • There's a speed difference in single & double quotes!?
    – JakeParis
    Jan 28, 2011 at 14:51
  • +1 for the part about the variable disappearing. Good thought!
    – JakeParis
    Jan 28, 2011 at 14:52
  • 2
    @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, 2011 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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