PHP &$string - What does this mean?

PHP &$string - What does this mean?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP &$string - What does this mean?

When you encounter code like $x->func(&$string, $str1=false);, the ampersand (&) placed before a variable parameter is one of the most distinct features of PHP's type system. It denotes that the function expects an argument passed by reference. This is not merely syntactic sugar; it fundamentally alters how data flows between the caller and the callee, impacting memory management and application logic in ways that can be subtle but critical.

In standard PHP function calls, arguments are typically passed by value. When you pass a variable to a function without the & prefix, the engine creates a copy of the variable's current value. If the function modifies that parameter internally, those changes remain isolated within the function's scope and do not affect the original variable outside. Consider this standard behavior:

function modifyString($var) {
    $var .= " modified";
}

$original = "Hello";
modifyString($original);
// $original remains "Hello"

The ampersand changes this contract entirely. By declaring a parameter as &$string, you are instructing PHP to pass the memory address (reference) of the original variable rather than copying its value. Inside the function, $string and $original point to the exact same location in memory. Any modification performed on the parameter inside the function immediately reflects in the caller's scope.

function modifyStringByRef(&$var) {
    $var .= " modified";
}

$original = "Hello";
modifyStringByRef($original);
// $original is now "Hello modified"

This mechanism is particularly valuable when you need to return multiple values from a single function. PHP functions can technically only return one value, but by using references in the parameter list, you can effectively update several variables at once and have them populated upon execution.

function calculateCoordinates(& $x, & $y) {
    $x = 10;
    $y = 20;
}

$lat = 0;
$lon = 0;
calculateCoordinates($lat, $lon);
// Both $lat and $lon are updated without needing a return statement

However, passing by reference introduces specific pitfalls that experienced engineers must navigate. The primary concern is accidental modification of state. If a function accepts a reference parameter but does not intend to modify the caller's variable, passing it by reference can lead to bugs where external data structures are unexpectedly altered. Developers often mistake this for passing by value, especially when the function signature isn't immediately visible or when using dynamic calls like call_user_func.

Furthermore, you cannot pass a literal value directly into a reference parameter in PHP; the variable must hold a valid referenceable entity. Attempting to assign a string constant directly to a reference parameter will result in a fatal error because constants are not modifiable variables.

// Fatal Error
function setVal(&$var) { $var = "new"; }
setVal("literal"); // Fails: Cannot pass non-variable to reference parameter

In modern PHP development, particularly within frameworks like Laravel, this syntax is less common for simple data manipulation but remains essential for specific architectural patterns. For instance, dependency injection containers or complex service factories often rely on reference parameters to bind instances tightly without returning them explicitly. If you are working with legacy codebases or interacting with third-party libraries that predate modern PHP standards, understanding &$string is vital for debugging why a string appears mutated unexpectedly after a function call.

The trade-off here is clarity versus flexibility. While it allows for powerful in-place modifications, it reduces the transparency of side effects unless the developer explicitly intends to mutate state. Always check whether a function signature declares a reference parameter before assuming immutability, as this can silently break assumptions about data integrity.

Our network