Glossary

Pass by Reference (&)

In PHP, variables are passed by value by default: the function receives a copy. When passed by reference (&$var), the function works with the original variable.

Example

function addTen(int $n): void {
  $n += 10; // modifies the local copy only
}
function addTenRef(int &$n): void {
  $n += 10; // modifies the original
}

$x = 5;
addTen($x);    // $x = 5
addTenRef($x); // $x = 15

Objects — a special case

Objects in PHP are passed by handle (a pointer to the object), not by value or reference. Modifying an object's properties inside a function is visible outside. But $obj = new OtherClass() is not (it replaces the local handle only).

When to use

Rarely needed in modern code. Prefer returning a new value from the function rather than mutating an argument. Justified for sort()-style functions or large structures to avoid copying.