Jump to content

Recommended Posts

NULL is a value (or rather lack thereof), is_null () is a function (language construct, really) that checks a variable and returns true if it contains NULL. Read more about it in the PHP manual.

 

Thank you very much for the helpful reply!

it is faster to do:

 

if($value === null){
    // do something:
}

 

over

 

if(is_null($value)){
    // do something:
}

 

 

Iterations: 100000  (10 Runs)
#  | NULL     | rel %  | is_null() | rel %
----+----------+--------+-----------+--------
  1 |  0.02851 |  5.9 % |   0.48044 | 1685.4%
  2 |  0.03086 |  6.5 % |   0.47557 | 1541.2%
  3 |  0.02853 |  5.9 % |   0.48534 | 1700.9%
  4 |  0.02860 |  6.0 % |   0.47666 | 1666.6%
  5 |  0.02854 |  6.0 % |   0.47783 | 1674.3%
  6 |  0.02863 |  6.1 % |   0.47144 | 1646.7%
  7 |  0.02854 |  6.0 % |   0.47272 | 1656.2%
  8 |  0.02855 |  6.0 % |   0.47754 | 1672.6%
  9 |  0.02854 |  6.0 % |   0.47301 | 1657.5%
10 |  0.02853 |  6.0 % |   0.47380 | 1660.7%

it is faster to do

 

is_null involves a function call which is a relatively costly operation in PHP, that is why there is a speed difference.  I tend to use is_null anyway out of habit and I like the way it reads better when I skim code.  In practice it  generally makes little difference as you're probably not going to be calling it thousands of times.

 

Either method accomplishes the same task so use whichever you prefer.

 

Are those numbers microseconds, or nanoseconds? Either way, you don't want to teach newbies about micro-optimization like this. Point in case, even if the numbers are microseconds, you're still only saving 0.0000046th of a second for every check you do and you don't tend to do a whole lot of them.

 

The best course of action is to use whatever makes the code the easiest to read, as that'll help you save a lot more time (in development and maintenance) than it'll ever use in production.

 

Here is the source:

http://hakre.wordpress.com/2011/03/29/php-is_null-vs-null/

 

That should also be time in seconds

 

The comment on there

"There is also a difference between NULL === $var and $var === NULL"

Annoys me. There is a difference? What is it? If you already figured out that there's a difference, post your work, man!

Here is the source:

http://hakre.wordpress.com/2011/03/29/php-is_null-vs-null/

 

That should also be time in seconds

 

The comment on there

"There is also a difference between NULL === $var and $var === NULL"

Annoys me. There is a difference? What is it? If you already figured out that there's a difference, post your work, man!

 

 

I saw that too, so I had to try.... and... their wasn't a difference.

I've linked to this article before, explaining why micro-optimization generally isn't worth it. Especially not when you have no idea whether or not the code in question will actually be a bottle neck.

 

There's also more information in another article, about Premature Optimization, which I'm afraid your advice classifies as.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.