4

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?

Ruben
  • 65
  • 6
  • 1
    Best people to ask that question are the PHP development team.... they're the only ones who can explain why they chose to make that decision, the rest of us can only speculate – Mark Baker Sep 19 '13 at 17:52
  • 1
    My guess is that's because of how references work. You're setting `$a` and `$b` to point to the same memory location. – Rocket Hazmat Sep 19 '13 at 17:54

1 Answers1

3

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.

Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
Jonathan Kuhn
  • 14,619
  • 2
  • 28
  • 41