Are PHP zvals mutable?

38 Views Asked by At

I've been reading about memory management in PHP and learned that variables in PHP copy the reference to zvals as long as you don't do a write operation (copy on write paradigm).

https://www.phpinternalsbook.com/php5/zvals/memory_management.html#reference-counting-and-copy-on-write

However, it does not describe what happens when you reassign a value to an already copied zval.

Here's what the book says:

$a = 1;    // $a =           zval_1(value=1, refcount=1)
$b = $a;   // $a = $b =      zval_1(value=1, refcount=2)
$c = $b;   // $a = $b = $c = zval_1(value=1, refcount=3)

$a++;      // $b = $c = zval_1(value=1, refcount=2)
           // $a =      zval_2(value=2, refcount=1)

Now, if you do another $a++, will it change the value in zval,

$a++; // $a = zval_2(value=3, refcount=1)

or will it create a new zval once again?

$a++; // $a = zval_3(value=3, refcount=1)

Following the logic of the PHP Language Reference, I guess it should be more likely option #1 as long as refcount = 1 (you would not manipulate another variable).

0

There are 0 best solutions below