If I make a copy of a reference variable. Is the new variable a pointer or does it hold the value of the variable the pointer was referring to?
TL;DR: PHP supports both pass by value and pass by reference. References are declared using an ampersand ( & ); this is very similar to how C++ does it.
The clone keyword is used to create a copy of an object. If any of the properties was a reference to another variable or object, then only the reference is copied. Objects are always passed by reference, so if the original object has another object in its properties, the copy will point to the same object.
For passing variables by reference, it is necessary to add the ampersand ( & ) symbol before the argument of the variable. An example of such a function will look as follows: function( &$x ). The scope of the global and function variables becomes global. The reason is that they are defined by the same reference.
In PHP, there is a shortcut for appending a new string to the end of another string. This can be easily done with the string concatenation assignment operator ( . = ). This operator will append the value on its right to the value on its left and then reassign the result to the variable on its left.
It holds the value. If you wanted to point, use the &
operator to copy another reference:
$a = 'test';
$b = &$a;
$c = &$b;
Let's make a quick test:
<?php
$base = 'hello';
$ref =& $base;
$copy = $ref;
$copy = 'world';
echo $base;
Output is hello
, therefore $copy
isn't a reference to %base
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With