The default value of variables in PHP?

I did a search on this question, but I could not find the answer to my question.

When a variable is declared without a value, for example:

$var; public $aVar; 

Is the value of the variable unknown, as in many languages ​​(i.e., everything that was in memory before), or is the variable set to null by default?

+7
source share
1 answer

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

However, just doing $var; will not declare a variable, so you can declare a variable without a value in the 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 
+11
source

All Articles