Search code examples
phpreferencesymbol-table

Strange behaviour, assigning undefined variable by reference


I am just exploring how Symbol Tables and Variable Containers work together with references. And I found out that

<?php    
   $a = & $b;    
?>

doesn't throw a Notice saying "Undefined variable: b in...", while

<?php    
   $a = $b;    
?>

does.

Why?


Solution

  • From the manual: http://php.net/manual/en/language.references.whatdo.php

    Note: If you assign, pass, or return an undefined variable by reference, it will get created.

    As to why, I would just be speculating that php allocates the memory and assigns $a and $b to both look at that spot in memory. It is a documented behavior though.