Null values in PHP

5

In PHP 7 what is the correct way to define and identify null values?

Which of these is the correct way to assign null?

$valor = null;

$valor = "";

Which of these is the correct way to identify null values?

if($valor == null){}

if($valor == "") {}

if($valor === null) {}
    
asked by anonymous 20.09.2017 / 15:34

4 answers

9

See the documentation .

This is a null:

$valor = null;

This is an empty string , nothing null:

$valor = "";

So it compares null, but an empty string is considered null as well, it's a complete non-sense , but PHP looks like this:

if($valor == null){}

This checks to see if it is empty string :

if($valor == "") {}

It is often considered a better way to buy, but in this case it makes no real difference:

if($valor === null) {}

You can also do:

if (is_null($valor)) {}

Documentation .

Or:

if (empty($valor)) {}

Documentation .

You'll find a difference in cases like this:

$a = array();
echo $a == null; //true
echo $a === null;  //false

It's so confusing, that I would avoid using nulls in PHP.

         empty    is_null 
         ==null  ===null  isset   array_key_exists
      ϕ |   T   |   T   |   F   |   F   
   null |   T   |   T   |   F   |   T   
     "" |   T   |   F   |   T   |   T   
     [] |   T   |   F   |   T   |   T
      0 |   T   |   F   |   T   |   T      
  false |   T   |   F   |   T   |   T   
   true |   F   |   F   |   T   |   T   
      1 |   F   |   F   |   T   |   T   
     
$valor = null;
| F | F | T | T

Table taken from OS response .

At least in "normal" languages it looks like this:

    
20.09.2017 / 15:41
2

To set is null and to check it out with is_null($valor); which returns a true or false (Boolean) value if it is NULL would not indicate the other shapes that even work, but which occasionally bring unexpected results, so always check with the responsible function for this, example:

Code:

<?php

    $valor = null;    
    var_dump(is_null($valor));
    echo PHP_EOL;
    var_dump(($valor) == null);
    echo PHP_EOL;
    var_dump(($valor) === null);
    echo PHP_EOL;

Output:

bool(true)

bool(true)

bool(true)

gave the same answer.

ONLINE Sample

Single or double quotation marks with no spaces means string empty and should be checked with empty ($ value) , example:

Code:

<?php

    $valor = "";

    var_dump(empty($valor));
    echo PHP_EOL;
    var_dump($valor=="");
    echo PHP_EOL;
    var_dump($valor==="");
    echo PHP_EOL;

Output:

bool(true)

bool(true)

bool(true)

which also gave the same answer, ie will work the 3 ways displayed, but, always use if there is in the language its functions responsible for this.

    
20.09.2017 / 15:41
1

When you use "" this it is not null but an empty string.

Use this command that takes the type of the variable for you to understand better:

echo gettype("");
echo gettype(null);

The first one will return string and the second null

While the comparison when you use == you compare only the value for example:

if("2" == 2)

This will return true since both are the value two.

but if you use:

if("2" === 2)

It will be false since the values are two, but one is a string and the other is a numeric value, ie === compare the value and also the type of the variable.

    
20.09.2017 / 15:47
0

I think the best way is with false , since it has no type restriction

$valor = [0, null, false, ''];

foreach($valor as $v){
  if($v == false) echo 'null';
}

/*
Resultado 

null
null
null
null

*/
    
20.09.2017 / 15:43