Why is a variable with no value considered uninitiated?

Asked

Viewed 860 times

10

I realized that PHP returns that a variable has not been started if it has no value assigned to it. In my opinion, it makes no sense because the variable has already been started and awaits a value.

Example:

class Users {
    public $username;

    public function __construct() {
        if(isset($this->username))
            echo 'variável foi iniciada';
        else
            echo 'variável não foi iniciada';
    }
}

Why does that happen?

5 answers

11


If parameters are passed, then isset will return TRUE, only if all parameters are defined.

An uninitiated variable is equivalent to the PHP constant NULL. Hence the function isset will return FALSE.

If you are dealing with the properties of the object and want to "evaluate" the value of NULL, you can use: property_exists instead of isset:

<?php

class minhaClass{
    public $mine;
    private $xpto;
    static protected $teste;

    function teste() {
        var_dump(property_exists($this, 'xpto')); //true
    }
}

var_dump(property_exists('minhaClass', 'mine'));   //true
var_dump(property_exists(new minhaClass, 'mine')); //true
var_dump(property_exists('minhaClass', 'xpto'));   //true, para PHP 5.3.0
var_dump(property_exists('minhaClass', 'bar'));    //false
var_dump(property_exists('minhaClass', 'test'));   //true, para PHP 5.3.0
minhaClass::teste();

?>

To complement, see this table below: F = false T = true

        isset  is_null ===null  ==null  empty
 null |   F   |   T   |   T   |   T   |   T   |
unset |   F   |   T   |   T   |   T   |   T   |
  ""  |   T   |   F   |   F   |   T   |   T   |
  []  |   T   |   F   |   F   |   T   |   T   |
    0 |   T   |   F   |   F   |   T   |   T   |
false |   T   |   F   |   F   |   T   |   T   |
 true |   T   |   F   |   F   |   F   |   F   |
    1 |   T   |   F   |   F   |   F   |   F   |
   \0 |   T   |   F   |   F   |   F   |   F   |

8

There is a difference between declaring a variable and assigning a value to it.

Declaring means that you have specified that a variable will exist in a given context. But that’s it. In PHP if the variable does not have an assigned value, it is null.

Only when you assign a value does the variable become usable.

In your example the variable was only declared and not initialized.

3

I believe it is only a problem of nomenclature and understanding of the method isset.

Of documentation:

Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will Return FALSE if testing a variable that has been set to NULL. Also note that a null Character (" 0") is not equivalent to the PHP NULL Constant.

You mentioned the word "initiated". In common understanding of the word, an initiated variable is a variable that had some value assigned. In the example shown by you, this did not occur effectively.

But confusion is normal. In the Java world, a variable can be considered started by assigning the value null in it, different from the method isset PHP that does not consider a null variable as started.

1

The variable is only one reference to a valor/objeto.

There is a difference between declaring a variable and initializing a variable. In your example you declared the variable but did not initialize it. That is, you did not assign any valor/objeto to her. So her value is still null.

1

In PHP, variables are evaluated as false for isset in two cases:

  • When it doesn’t exist
  • When she’s null.

For the two cases below, the evaluation of isset will return false.

var_dump(isset($a));

$b = null;


var_dump(isset($b));

Additional detail:It is unrelated to the question, but it is important to note that in case of verification of arrays, is not recommended for some cases the use of isset, but rather of array_keys_exists.

Example 1:

$a = array(1 => 'um', 2 => null);

var_dump(isset($a[1])); // bool(true);

var_dump(isset($a[2])); // bool(false);

Example 2:

var_dump(array_keys_exists(1, $a)); // bool(true);

var_dump(array_keys_exists(2, $a)); // bool(true);

As has been said, for values null, isset will return false also. Note that, array_keys_exists checks the existence of the index, rather than checking whether the value is null.

Browser other questions tagged

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