Why is a non-value variable considered uninitialized?

9

I noticed that PHP returns that a variable was not started if it has no value assigned to it. In my opinion, it does not make sense because the variable has already started and waits for 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';
    }
}

What is the reason for this?

    
asked by anonymous 25.08.2015 / 02:00

5 answers

11

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

An uninitialized variable is equivalent to the PHP NULL constant. Then the function isset will return FALSE .

If you are dealing with object properties 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   |
   
<?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();

?>
| T | F | F | F | F |
    
25.08.2015 / 02:15
8

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

Declaring means that you have indicated that a variable will exist in a given context. But only this. In PHP if the variable does not have a value assigned, 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.

    
25.08.2015 / 02:16
3

I think it's just a problem of naming and understanding the isset method.

From documentation :

  

Determine if a variable is set and is not NULL.

     

If the 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 "started". In common understanding of the word, a variable started is a variable that had some value assigned . In the example shown by you, this did not actually happen.

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

    
25.08.2015 / 02:42
1

The variable is just a 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 it. So its value is still null .

    
25.08.2015 / 13:46
1

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

  • When it does not exist
  • When it is 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 not related to the question, but it is important to note that, in the case of checking indexes of arrays , it is not recommended for some cases to use isset but 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 I said, for null values, isset will return false as well. Note that, array_keys_exists checks for the existence of the index, rather than verifying that the value is null .

    
25.08.2015 / 14:00