raviolican raviolican - 4 months ago 8
PHP Question

Does a reference to and instance create a new identity

I have

$var1 = 123
$var2 = $var1


Does var2 reference to the identity of the instance 123 or does it copy the instance and creates a new reference in PHP?

Answer

$var2 = $var1; does not create a reference. It will indeed be copied.

If you do want a reference, you can do it like this:

$var2 = &$var1;

Further reading

Comments