=& operator, memories

memory, php, reference

Solution

Never ever use references in PHP just to reduce memory load. PHP handles that perfectly with its internal copy on write mechanism. Example:

$a = str_repeat('x', 100000000); // Memory used ~ 100 MB
$b = $a;                         // Memory used ~ 100 MB
$b = $b . 'x';                   // Memory used ~ 200 MB

You should only use references if you know exactly what you are doing and need them for functionality (and that's almost never, so you could as well just forget about them). PHP references are quirky and can result to some unexpected behaviour.

Problem

I am very confused about how using & operator to reduce memories. Can I have an answer for below question?? ``` clase C{ function B(&$a){ $this->a = &$a; $this->a = $a; //They are the same here created new variable $this->a?? // Or only $this->a = $a will create new variable? } } $a = 1; C = new C; C->B($a) ``` Or maybe my understanding is totally wrong.....

Original source

Related problems