Default value of variables in PHP?

后端 未结 1 689
星月不相逢
星月不相逢 2021-01-12 05:30

I did a search for this, but I could not find an answer to my question.

When a variable is declared without a value, like this:

$var;
public $aVar;
<         


        
相关标签:
1条回答
  • 2021-01-12 06:07

    Variables that are declared without a value and undefined/undeclared variables are null by default.

    However, just doing $var; will not declare a variable so you can only declare a variable without a value in an object.

    Demo:

    <?php
    class Test { public $var; }
    $var;
    $t = new Test();
    var_dump($var);
    var_dump($t->var);
    

    Output:

    Notice: Undefined variable: var in - on line 5
    NULL
    NULL
    
    0 讨论(0)
提交回复
热议问题