In PHP, when a function or method returns a value, it can either return a copy of the value (by value) or a reference to the original value (by reference).
When a function returns a value by reference, it means that the function is returning a pointer to the original value, rather than a copy of that value. This means that any changes made to the returned value will be reflected in the original value, since both are pointing to the same memory location.
You can return a value by reference by appending an ampersand (&) to the function name when defining the function, like this:
function &someFunction() { $a = 5; return $a; }
You can also return a reference to a variable by using the reference operator (&) before the variable name when returning it, like this:
$a = 5; function &someFunction() { global $a; return $a; }
When a function returns a value by value, it means that the function is returning a copy of the value, rather than a reference to the original value. This means that any changes made to the returned value will not be reflected in the original value, since they are different copies.
function someFunction() { $a = 5; return $a; }
This is the default behavior of the function in PHP. when you are not using any & operator while returning a value it means you are returning by value
It’s worth noting that in PHP, certain types (such as integers and floating-point numbers) are passed by value by default, while other types (such as arrays and objects) are passed by reference by default.
It’s also worth noting that, when passing an array or object as an argument to a function, any changes made to the passed array or object inside the function will be reflected in the original value, regardless of whether the argument is passed by value or by reference. This behavior is similar to the one you see with returning by reference, and you can use the same reference operator & before the variable name in function argument to make sure you are passing it by reference.