Jump to content

Recommended Posts

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" 8)

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368883
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368895
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368903
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368905
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368923
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368950
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368955
Share on other sites

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:

 

table.jpg

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1368972
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369043
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369044
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369189
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369521
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369568
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369575
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266971-null-vs-null/#findComment-1369586
Share on other sites

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.