Difference between $ value == null and null == $ value

2

What is the difference of putting the null in front of or behind the variable that we are comparing, that is, what is the difference between: if ($value==null){...} and if (null==$value){...} I've heard that the second option is more efficient from the compiler's point of view, to tell the truth, I do not know what the difference is.

    
asked by Jaime Roman 04.04.2018 в 18:39
source

3 answers

2

What is the difference between putting the null in front of or behind the variable we are comparing?

There is no difference except a syntax convention. This is true both for the one who writes the code to avoid typing errors, as for the Parser (at least in PHP) that tokenizes the source code to assemble the syntax tree that is then compiled to bytecode that then interprets it to opcode (assembler from the platform)

A constant such as number 42 or null is a token that has a rule (in the parser) by which it can not be followed by an assignment token (un =) but by a comparison token (a == )

Why is it being used or recommended?

I explain with the number 42 to make the effect of this practice clearer. in their homes they can use null

in PHP (and other languages) is generally used to avoid assignment errors.

#!/usr/bin/env php
<?php

$value = 40;

if ($value=42) :
  echo "$value es 42".PHP_EOL;
else :
 echo "$value NO es 42".PHP_EOL;
endif;

Result:

$ ./yodacondition.php
42 es 42

It's a simple example and we can infer the value of $value before if is 40 so the typing error (put = instead of == ) is evident.

But this error would not be so evident if $ value comes from another side.

Instead using Yoda Conditions ( link )

#!/usr/bin/env php
<?php

$value = 40;

if (42=$value) :
  echo "$value es 42".PHP_EOL;
else :
 echo "$value NO es 42".PHP_EOL;
endif;

Result:

$ ./yodacondition2.php
PHP Parse error:  syntax error, unexpected '='

This is because 42 is a constant and can not be assigned a value.

What the compiler optimizes seems to me to be an urban myth, since the compiled languages usually have optimizers that create comparison tables and conditional jump internally for this type of if then else.

Edit 2: in the first example $value=42 not only assigns the value 42 to $value but returns true satisfying the condition of if

    
answered by 04.04.2018 / 19:06
source
4

Given the number of answers (three) that I think do not answer the question, I did a little test, to see if either of them is more "fast" or "efficient":

  

Consists of executing 1 million times if(null == $var){//} and if($var == null){//}

     

The column with name 'null' is that of if(null == $var){//} , and the column with name 'var' is that of if($var == null){//}

Iteraciones: 1000000  (20 ejecuciones)
 #  | null     | rel %   |  var      | rel %  
----+----------+---------+-----------+--------
  1 |  0.21405 |  97.2 % |   0.22014 |  102.8%
  2 |  0.21474 | 104.2 % |   0.20613 |   96.0%
  3 |  0.22034 | 103.9 % |   0.21200 |   96.2%
  4 |  0.21678 |  96.4 % |   0.22485 |  103.7%
  5 |  0.28206 | 138.0 % |   0.20432 |   72.4%
  6 |  0.37546 |  91.6 % |   0.41001 |  109.2%
  7 |  0.39286 |  98.8 % |   0.39760 |  101.2%
  8 |  0.38703 |  97.7 % |   0.39617 |  102.4%
  9 |  0.38722 |  96.7 % |   0.40030 |  103.4%
 10 |  0.33443 |  82.9 % |   0.40357 |  120.7%
 11 |  0.20490 |  87.2 % |   0.23485 |  114.6%
 12 |  0.19351 | 100.7 % |   0.19219 |   99.3%
 13 |  0.19576 |  97.3 % |   0.20112 |  102.7%
 14 |  0.19199 |  88.9 % |   0.21584 |  112.4%
 15 |  0.20707 | 100.5 % |   0.20597 |   99.5%
 16 |  0.20554 | 104.3 % |   0.19706 |   95.9%
 17 |  0.19500 |  95.1 % |   0.20504 |  105.1%
 18 |  0.19618 | 101.5 % |   0.19333 |   98.5%
 19 |  0.19848 | 102.1 % |   0.19432 |   97.9%
 20 |  0.21158 | 101.8 % |   0.20785 |   98.2%

In my opinion, there is practically no difference in terms of 'speed' (taking into account that there are a million iterations).

Just in case, the test was done with php 7.1.7

    
answered by 04.04.2018 в 19:37
1

This I read several times, it is not a matter of efficiency, it is a practice that can be applied to avoid making the following type of error when making comparisons:

if(variable == 5) //bien escrito

if(5 == $variable) //bien escrito

if(variable = 5) //No es lo que queríamos pero esta bien escrito y variable ahora vale 5, 

if(5 = $variable) // Tira Error

Therefore writing it "upside down" we make it pull error and not keep working and on changing the value of my variable if we "forget" the "==".

    
answered by 04.04.2018 в 19:00