Null values in PHP

Asked

Viewed 2,092 times

8

In PHP 7 which 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) {}

4 answers

13


See in itself documentation.

This is a null:

$valor = null;

This is a string empty, nothing at all:

$valor = "";

So compares null, only one string empty is null too, is a complete non-sense, but PHP is like this:

if ($valor == null) {}

This checks if it is a string empty:

if ($valor == "") {}

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

if ($valor === null) {}

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

I put in the Github for future reference.

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

Empty() / ==null is_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
\0 F F T T

Table taken from response from the OS.

At least in "normal" languages:

NULL != 0

  • I know you can not comment here atoa, but I like this photo from the example kkkkkkkk

  • @Leandrosilvacampos is not so hard not.

  • Illustration show. kkkkk

2

To define is with null and to check is with is_null($value); returning a true or false value (boleano) whether or not it is NULL would not indicate the other ways that even work, but, that from time to time bring unexpected results, then 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;

Exit:

bool(true)

bool(true)

bool(true)

gave the same answer.

ONLINE EXAMPLE

Single or double quotes without space mean string empty and must 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;

Exit:

bool(true)

bool(true)

bool(true)

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

1

I believe the best way is with false, because it has no type restriction

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

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

/*
Resultado 

null
null
null
null

*/

1

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

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

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

The first 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 the two 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, that is === compares the value and also the type of the variable.

Browser other questions tagged

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