Why in so many ways check if a value is NULL? How to standardize?

8

There are certain things that still do not come into my head in relation to PHP.

Because there is the is_null function, when we can simply compare the values through a comparison operator. Such as $variable === NULL ?

And why in some tutorials recommend the "flipping" of this check.

That is, instead of using

if ($variable === NULL) {
   // faça alguma coisa
}

Teach you how to use:

if (NULL === $variable) {

  // faça alguma coisa
}

But in some frameworks , such as Laravel, I see a lot:

if ( is_null($variable)) {

     // faça alguma coisa   
}

They say it's faster to do as in the second example.

But is this really something I should worry about? Or is it just a ridiculous micro-optimization?

Should I stop putting is_null in my codes, since a comparison operator consumes less resource in memory than a function call?

Is there a standard (some FIG Standard) that helps with this task, to facilitate standardization?

    
asked by anonymous 24.02.2015 / 13:14

3 answers

5

The function is_null() does the same as NULL === .

Both do the same thing, but in relation to speed, NULL === is faster, 14 times faster (according to this comparison here ). Regarding the various ways of doing this, each mode has its particularity, for example the function isset returns true only when a variable is not null, empty by then returns true if a variable is an empty string , false , NULL . is_null() is the opposite of isset , except isset may be applicable to unknown variables, while is_null() to declared variables.

Seealso: Type Comparison Table in PHP

    
24.02.2015 / 14:08
6

There are several ways. Or they exist if you consider that any code can be written in several ways.

The second form is exactly equivalent to the first form, especially in performance. It is called yoda condition as you have already discovered. In theory, it should help to discover errors in languages that allow assigning value to variables where conditional expressions are normally expected by checking for equality (many languages do not allow). The idea would be to cause an error by doing this:

if (null = $var)

If you write the "normal way" this would happen:

if ($var = null)

and $var would receive null and continue executing the code ( but would not enter if because $var would be null which is considered a false .

Write

if(null === $var)

has no advantage whatsoever. It would only be advantageous if you make a mistake and forget two of these equal signs because you would have a mistake.

But honestly if you think you can make this kind of mistake and do not realize I think it will be even worse to forget to use === instead of == as is almost always recommended. And there's nothing to stop you from making the mistake of "eating" just one character of the same. And if you usually use three equals, the chance to change from three to one character is much smaller.

Remembering that this can avoid a typo but not an error due to ignorance of how to use the right operator.

is_null is a form that in theory is slower but can leave more clear and avoid the above problems. I particularly prefer the form of operator but I can understand that some codes are required to use the function to avoid misunderstandings. As always the important thing is to maintain consistency. Everyone should standardize as they wish.

    
24.02.2015 / 13:52
3

The answer is much simpler than it seems, there is no difference, this is one of the small problems of PHP, it was not written under a standardized specification, in fact I do not even know how its functions appeared, in this case you can use what you please, deep down there is a small difference in nano seconds but it will hardly make a difference. Now the fact is that this function will hardly disappear because they probably do not want to create legacy codes because of trivial things like that.

    
24.02.2015 / 13:50