Hecksa
Hecksa

Reputation: 2842

Which is quicker - a boolean variable check or is_null()?

Suppose I'm creating a session class, with relevant implementation as below:

public class Session()
{
    private $id;
    private $user;
}

The $user field contains an object of type User if the session is logged in, and is null if the session is not logged in to the site. The $id is the session id.

Suppose I now want to find out whether or not the user is logged in. Obviously I could check to see if $user is null, if it is then the user isn't logged in - something like this:

public class Session()
{
    private $id;
    private $user;

    public function isLoggedIn()
    {
        return !is_null($user);
    }
}

Alternatively, I could store a boolean session variable, $loggedIn or something, set to false on log in and otherwise initialised in the constructor to be false to test against instead:

public class Session()
{
    private $id;
    private $user;
    private $loggedIn;

    public function isLoggedIn()
    {
        return $loggedIn;
    }
}

Would one approach produce better performance than the other here? If so, which, and why? Alternatively, is one approach preferable to the other for any reason unrelated to performance?

Upvotes: 3

Views: 3505

Answers (5)

hakre
hakre

Reputation: 197943

You actually can solve your issue with a cast to bool:

public function isLoggedIn()
{
    return (bool) $this->user;
}

Every object is TRUE, and NULL is FALSE in PHP.

Your original question about "what is faster" does normally not play any significant role. What's more important is that you can easily read your code which includes not writing needless code, especially not because you guess something might be faster when the moon is in a specific phase.

You don't know the two important things: Is my code too slow? Which one is faster?. If you can't answer the first question at all, well then don't even think about asking the second.

You're only putting something in your brain that you will never use. That's a common mistake inexperienced programmers do, so the earlier you stop with that, the faster you'll get results you can work with. And the more easy you keep your code from the beginning, into the less bottlenecks you will run. And those you'll run into won't be a problem for you because there is not much to change to remove performance hogs. Sounds fair?

Upvotes: 1

FtDRbwLXw6
FtDRbwLXw6

Reputation: 28899

The fastest method would be isset():

public function isLoggedIn() {
    return isset($this->user);
}

The isset() function returns TRUE if the variable is set (exists) and is not NULL, or FALSE otherwise.

It's much faster than !is_null() and !empty(), and marginally faster than !== NULL, both when the value is NULL, as well as when it is not NULL.

Upvotes: 1

Alex Howansky
Alex Howansky

Reputation: 53573

Simpler is better. If you introduce a new variable to track whether or not $user is null, then you also introduce the possibility that is_null($user) and $loggedIn don't match. Avoiding the hassle of having to worry about that is worth much more than any possible micro-optimization you may gain in performance.

Upvotes: 6

khael
khael

Reputation: 2610

Checking php.net for a benchmark at this user contributed post (link to post) we see that == and === are much faster than is_null.

Upvotes: 11

user215361
user215361

Reputation:

I'd say the best way to do this would be:

return ($user !== null)

Upvotes: 3

Related Questions