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

Asked

Viewed 972 times

7

There are certain things still that do not enter my mind regarding PHP.

Because there is a function is_null, when we can simply compare the values using a comparison operator. For example $variable === NULL?

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

I mean, instead of using

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

Teach to use:

if (NULL === $variable) {

  // faça alguma coisa
}

But in some frameworks, like Laravel, I see a lot:

if ( is_null($variable)) {

     // faça alguma coisa   
}

They say it’s quicker to do as in the second example.

But is that really something I should be worried about? Or is it just a ridiculous microtimization?

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

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

  • 1

    The second example calls if I am not mistaken check Yoda, serves for you to exchange the comparison for assignment in languages where it is used = for both of us.

  • "Yoda conditions" for the intimate (I just saw on Wikiedia)

3 answers

6

There are no several ways. Or there are if you consider that any code is possible to write in several ways.

The second form is exactly equivalent to the first form, mainly 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 usually expected by checking equality (many languages do not allow it). The idea would be to make a mistake by doing this:

if (null = $var)

If you write in the "normal way" it would pass:

if ($var = null)

and $var would receive the value null and would continue the execution of the code (but would not if because $var valeria null which is considered a false.

Write down

if(null === $var)

does not bring any advantage. It would only be advantageous if you mistake and forget two of these signs of equal because you would have a mistake.

But honestly if you think you can make this kind of mistake and not realize I think it will be worse to forget to use === in place of == as is almost always recommended. And there is nothing to prevent you from making the mistake of "eating" just one character of equal. And if you always use three of a kind, the chance of changing from three to the same character is much less.

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

is_null is a form which in theory is slower but which can make it clearer and avoid the above problems. I particularly prefer the form of operator but I can understand that in some codes it is required to use the function to avoid misunderstandings. As always the important thing is to maintain consistency. Each should standardize as you want.

5


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

They both do the same thing, but in terms of speed, NULL === is faster, 14 times faster (according to this comparison here). In relation to 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 in turn returns true if a variable is a string empty, false, NULL. is_null() is the opposite of isset, except that isset may be applicable to unknown variables, while is_null() the declared variables.

inserir a descrição da imagem aqui

See also: Type comparison table in PHP

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 know how its functions were appearing, anyway in this case you can use what you like best, in the background 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 don’t want to create legacy codes because of trivial things like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.