Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Passing by reference in PHP binds a function parameter to the same internal zval (Zend Engine value container) as the argument provided by the caller. Instead of creating a copy of the value in the function’s local scope, PHP establishes an alias in the local symbol table that points to the original variable’s memory address. Consequently, any mutations applied to the parameter within the function directly alter the original variable in the calling scope.
Syntax
To declare that a function parameter must be passed by reference, prepend an ampersand (&) to the parameter name in the function signature.
function appendSuffix(string &$stringParam): void {
$stringParam .= "_suffix";
}
$data = "base_string";
appendSuffix($data);
echo $data; // Outputs: base_string_suffix
Technical Constraints and Behavior
1. Signature-Level Declaration
Passing by reference is strictly defined in the function signature. Call-time pass-by-reference was deprecated in PHP 5.3 and removed in PHP 5.4. The caller simply passes the variable normally, and the engine handles the reference binding based on the function definition. Attempting to pass by reference at call-time results in a compile-time syntax error.
// CORRECT: Reference defined in signature
function modifyArray(array &$arr): void {
$arr[] = 'new_element';
}
$myArray = ['initial_element'];
modifyArray($myArray);
// PARSE ERROR: Call-time reference is invalid syntax in PHP 5.4+
// modifyArray(&$myArray);
2. Variable Requirement
Because a reference requires a memory address to point to, you can only pass actual variables by reference. Passing literals, expressions, or function return values (unless the function returns by reference) will result in a fatal error.
function increment(int &$val): void {
$val++;
}
$num = 10;
increment($num); // Valid
// increment(10); // Fatal error: Cannot pass parameter 1 by reference
// increment($num + 5); // Fatal error: Cannot pass parameter 1 by reference
3. Default Values
Reference parameters can declare default values, but the default must be a valid constant expression. If the caller omits the argument, the function operates on a local variable initialized to the default value. In this scenario, a reference link to the calling scope is never established.
function setFlag(bool &$flag = false): void {
$flag = true;
}
setFlag(); // Operates on a local variable; no external reference exists
Returning by Reference
PHP also supports returning by reference, which allows a function to return a pointer to a variable rather than its value. This requires the ampersand (&) in two places: before the function name in the declaration, and before the function call during assignment.
class Configuration {
private array $settings = ['theme' => 'dark'];
// Ampersand before function name
public function &getSetting(string $key): string {
return $this->settings[$key];
}
}
$config = new Configuration();
// Ampersand before function call to bind the reference
$themeRef = &$config->getSetting('theme');
$themeRef = 'light';
// The internal $settings array is now ['theme' => 'light']
Unsetting References
Using the unset() construct on a reference parameter inside a function does not destroy the original variable in the calling scope. It only severs the binding between the local alias in the function’s symbol table and the underlying zval.
function destroyReference(&$param): void {
unset($param);
$param = "New Value"; // This creates a new local variable
}
$original = "Preserved";
destroyReference($original);
echo $original; // Outputs: Preserved
Master PHP with Deep Grasping Methodology!Learn More