eldan88 Posted September 3, 2012 Share Posted September 3, 2012 Hey I have a question? I just wanted to know the difference between both of them. Thanks Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 3, 2012 Share Posted September 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 4, 2012 Author Share Posted September 4, 2012 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! Quote Link to comment Share on other sites More sharing options...
salathe Posted September 4, 2012 Share Posted September 4, 2012 is_null () is a function (language construct, really) The parenthetical remark is incorrect, is_null is a perfectly normal function and not a language construct. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 Ah, you're right. Got it mixed up with isset (), sorry. Thanks for the correction. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 4, 2012 Share Posted September 4, 2012 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% Quote Link to comment Share on other sites More sharing options...
kicken Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2012 Share Posted September 5, 2012 Here is the source: http://hakre.wordpress.com/2011/03/29/php-is_null-vs-null/ That should also be time in seconds Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 5, 2012 Share Posted September 5, 2012 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! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
premiso Posted September 5, 2012 Share Posted September 5, 2012 The comment on there "There is also a difference between NULL === $var and $var === NULL" What is it? Obviously.... NULL has been switched with $var. *premiso rollseyes Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 5, 2012 Share Posted September 5, 2012 Christian, OP asked if there was a difference. The Little Guy's post shows a difference. That's all. Chill. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 I'm chilled, no worries. In fact, if I was any more relaxed I'd be sleeping. That's being 100% factual to boot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.