MySQL_Narb Posted August 12, 2012 Share Posted August 12, 2012 I personally feel like using: if($var === null) Although, I'm not too sure if I should be using: if($var == null) I understand it may just be preference, but is there a known performance difference between the two? Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/ Share on other sites More sharing options...
darkfreaks Posted August 12, 2012 Share Posted August 12, 2012 Example Name Result: $a == $b Equal TRUE if $a is equal to $b after type juggling. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368745 Share on other sites More sharing options...
carugala Posted August 12, 2012 Share Posted August 12, 2012 nice way to ignore NULL! iffin NULL doesn't exist? why use? Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368746 Share on other sites More sharing options...
Christian F. Posted August 12, 2012 Share Posted August 12, 2012 This is very well explained in the PHP manual, and I highly recommend reading it. carugala: What are you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368811 Share on other sites More sharing options...
maxudaskin Posted August 12, 2012 Share Posted August 12, 2012 nice way to ignore NULL! iffin NULL doesn't exist? why use? Instead of using a comparison, use empty(). Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368824 Share on other sites More sharing options...
darkfreaks Posted August 13, 2012 Share Posted August 13, 2012 i believe what he is looking for is if($variable=== NULL){} which will return NULL values only so will is_null however empty will return empty arrays,NULL,zero (0) & 0.0, empty variables however it will not check for empty spaces unlike isset , trim however isset will not check for NULL values. just for a more detail explanation besides "use empty it's good" Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368883 Share on other sites More sharing options...
darkfreaks Posted August 13, 2012 Share Posted August 13, 2012 speed wise: Using "===NULL" is 30x quicker than is_null however empty is a bit faster than than comparison operator or is_null Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368890 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 speed wise: Using "===NULL" is 30x quicker than is_null however empty is a bit faster than than comparison operator or is_null You missed this part: Micro optimization isn't worth it. You had to do it ten million times to notice a difference, a little more than 2 seconds $a===NULL; Took: 1.2424390316s is_null($a); Took: 3.70693397522s difference = 2.46449494362 difference/10,000,000 = 0.000000246449494362 The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368895 Share on other sites More sharing options...
maxudaskin Posted August 13, 2012 Share Posted August 13, 2012 speed wise: Using "===NULL" is 30x quicker than is_null however empty is a bit faster than than comparison operator or is_null You missed this part: Micro optimization isn't worth it. You had to do it ten million times to notice a difference, a little more than 2 seconds $a===NULL; Took: 1.2424390316s is_null($a); Took: 3.70693397522s difference = 2.46449494362 difference/10,000,000 = 0.000000246449494362 The execution time difference between ===NULL and is_null is less than 250 nanoseconds. You are correct. Most of us will likely never need to optimize such small numbers, but to teach good habit is crucial to, let's say, Facebook using 100,000,000 hours of processing time per year vs. 95,000,000 per year. Yes, those numbers are fictional, and yes, the statistics say that it's only 5%, but when you are talking about 5% over 100,000,000 hours, it's still 5,000,000 hours. 5,000,000 dollars. 5,000,000 starving children dead. 5,000,000 something. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368903 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 Sure, but shaving 250 nanoseconds off script execution isn't going to account for 5 million hours, that's just ridiculous. If you're worried about the performance of your application, profile it and fix the actual problem areas - don't waste time on something that makes no real-world difference. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368905 Share on other sites More sharing options...
carugala Posted August 13, 2012 Share Posted August 13, 2012 carugala: What are you talking about? null is unknown. an empty, oops, placeholder. ok, I'll know it when I see it! using null as a comparison, is pretty stoopid. 1 null has no relation to any other null. null was created from relational dbs, to cover when no data exists, outside join like. SELECT * FROM user, type, WHERE user.id=type.id. What if you want all users, regardless of type? Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368923 Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 carugala: What are you talking about? null is unknown. an empty, oops, placeholder. ok, I'll know it when I see it! using null as a comparison, is pretty stoopid. 1 null has no relation to any other null. null was created from relational dbs, to cover when no data exists, outside join like. SELECT * FROM user, type, WHERE user.id=type.id. What if you want all users, regardless of type? ... Then you'd use a left join, rather than an inner join (which BTW I think you did....) I don't think you understand the point of doing if($var === NULL){.... It's to check if the variable is null so you know what to PUT in the placeholder. (There are other reasons but this is an obvious one.) Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368950 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 carugala: What are you talking about? null is unknown. an empty, oops, placeholder. ok, I'll know it when I see it! using null as a comparison, is pretty stoopid. 1 null has no relation to any other null. null was created from relational dbs, to cover when no data exists, outside join like. SELECT * FROM user, type, WHERE user.id=type.id. What if you want all users, regardless of type? Null means that there is no value, and it is not stored in memory. Empty means that it has no value, but is stored in memory (as a 0 length string). Null is sometimes used to initialize or clear a variable. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368955 Share on other sites More sharing options...
Psycho Posted August 13, 2012 Share Posted August 13, 2012 This conversation is riddled with misinformation. The fact that one comparison is faster than another or suggesting to use a function is not appropriate unless and until you know EXACTLY the purpose of the condition to be checked: As stated before == null and === null are very similar, but not exactly the same. For example, if the value to be checked can be an empty string then the first comparison would return true and the latter false. The only way the latter comparison can be false, to my knowledge, is if the variable isn't set or it is explicitly set to null. All of these different comparisons will produce different results for some values. It is important to understand the purpose. For example, if the purpose is to check for a required field in POST data, empty() would be a poor choice since just a single space would cause the validation to pass. But, it could possibly be used in conjunction with trim()ing the value first. So, the discussion needs to first be what is the appropriate validation. Then if there are multiple, correct solutions then the discussion can be what is the most efficient. For illustrative purposes, here is a table showing the results using different comparisons with some possible values: Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368972 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 The only way the latter comparison can be false I think you meant true. Also, note that all of those "Unset" conditions will give a Notice: Undefined variable error. I know that that isn't the point, but it is good to know anyway. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368975 Share on other sites More sharing options...
Psycho Posted August 13, 2012 Share Posted August 13, 2012 The only way the latter comparison can be false I think you meant true. Also, note that all of those "Unset" conditions will give a Notice: Undefined variable error. I know that that isn't the point, but it is good to know anyway. On point #1, you are 100% correct. That is what I meant. Thanks for the catch! On point #2, you are about 80% correct. the empty() function will work, without errors/notifications, on an unset variable the same as the isset() function would. I always try to write code with full error reporting on and ensure no notifications of that type would occur. But, in reality, a production environment likely have a lower threshold for error reporting and would not display those. It would be funny to see what would occur on most sites with full error reporting on. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369043 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Share Posted August 13, 2012 On point #2, you are about 80% correct. the empty() function will work, without errors/notifications, on an unset variable the same as the isset() function would. Oops, I thought empty() choked too. But, in reality, a production environment likely have a lower threshold for error reporting and would not display those. Yeah, as long as you're not logging Notice's either. The log would probably be massive. I too code with all error reporting on, and I think every developer should. Leaving undefined variables or array indexes floating around is just sloppy in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369044 Share on other sites More sharing options...
MySQL_Narb Posted August 13, 2012 Author Share Posted August 13, 2012 Whoa! Definitely more discussion than I expected. I really appreciate the information! Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369128 Share on other sites More sharing options...
btherl Posted August 14, 2012 Share Posted August 14, 2012 Some people are passionate about nulls I've got a simple rule - use === whenever possible, because it keeps things simple. It's the right operator for nulls and string comparisons. == is suitable for comparing numbers only, when you want php to do the necessary conversions between strings, floats and integers. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369189 Share on other sites More sharing options...
carugala Posted August 15, 2012 Share Posted August 15, 2012 I don't think you understand the point of doing if($var === NULL){.... It's to check if the variable is null so you know what to PUT in the placeholder. (There are other reasons but this is an obvious one.) well, maybe I don't, but I believe I understand null quite well. In 1988, before null really existed, what did programmers use then? Null means that there is no value, and it is not stored in memory. Empty means that it has no value, but is stored in memory (as a 0 length string). Null is sometimes used to initialize or clear a variable. if not set in memory, how can test? nevermind, early php, only had concept of null to address DB access, and using php to retrieve said values, and now php is all nullified! now me pissing and lol. $var; actually declaring a var is not necessary in loosely typed php, initializing is good enuff. A php trait. regardless of PHP implementation, comparing nulls should be an exception(doh!), not common coding practice. what exactly is the binary code for null, if not stored in memory? This is an olde argument, but a good one.... Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369521 Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 And for a little bit more information, I wrote a whole article on the == operator Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369557 Share on other sites More sharing options...
Jessica Posted August 15, 2012 Share Posted August 15, 2012 function myFunc($arg1, $arg2=NULL){ if($arg2 == NULL){ $arg2 = array('Some other data', $arg1, 'more data'); } [...] return $retVal; } $myVal = myFunc(1); How would you do this sort of behavior without comparing against NULL? Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369568 Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 How would you do this sort of behavior without comparing against NULL? Initialize $arg2 to some value that's impossible for it to be. If you expect $arg2 to be a positive integer, make its default -1. That's how it was done before "null" became a language construct. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369575 Share on other sites More sharing options...
Jessica Posted August 15, 2012 Share Posted August 15, 2012 Ah good. Well it still seems like NULL has a good purpose there then. It's something you don't allow the argument to be. *shrug* Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369581 Share on other sites More sharing options...
scootstah Posted August 15, 2012 Share Posted August 15, 2012 function myFunc($arg1, $arg2=NULL){ if($arg2 == NULL){ $arg2 = array('Some other data', $arg1, 'more data'); } [...] return $retVal; } $myVal = myFunc(1); How would you do this sort of behavior without comparing against NULL? I've seen FALSE used for that. Or, sometimes you can just give it a default. I believe you can also give it an empty string. Quote Link to comment https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369586 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.