Let's understand the concept of call by reference by the help of examples. Variable $str is passed to the adder function where it is concatenated with 'Call By Reference' string.
Here, printing $str variable results 'This is Call By Reference'. You can pass a variable by reference to a function so the function can modify the variable.
function adder(&$str2)
{
$str2 .= 'Call By Reference example';
}
$str = 'This is ';
adder($str);
echo $str;
Output:
This is Call By Reference
Another example with nunber:
function increment(&$i)
{
$i++;
}
$i = 5;
increment($i);
echo $i;
Output:
6
What Is PHP Call by Reference?
Understand the difference
With ordinary argument passing, a function works with the value supplied to it. With PHP call by reference, a function parameter can refer to the caller’s variable, so an assignment made inside the function can change that original variable.

Use references intentionally
A reference parameter is part of a function’s contract. Anyone calling the function should be able to see that the function may modify an input. Use this feature for a clear reason instead of adding an ampersand automatically.
Basic Reference Parameter Syntax
Put the ampersand in the definition
The ampersand belongs before the parameter in the function definition, such as function updateValue(&$value). The call remains updateValue($value); there is no separate ampersand required at the call site.
Preserve the original example
In the existing string example, the function receives $str2 by reference and appends text to it. Because $str and $str2 refer to the same variable for that call, echoing $str after the function shows the updated string.
Use References With Numbers
Increment a caller variable
The increment example receives a number by reference and increases it inside the function. After increment($i), the caller sees the changed value. This is useful for a small counter, but a return value is often clearer for a calculation.
Compare mutation and return values
A function can instead accept a number normally and return the incremented result. The caller then assigns the result explicitly. Prefer that style when the operation is a transformation and there is no need to modify several outputs at once.
Pass Arrays by Reference
Add or update array data
An array can be passed by reference when a function is responsible for changing the caller’s collection. This can be suitable for a procedure that fills an output array or applies a documented update to a shared data structure.
Consider returning the array
Returning an updated array often makes data flow easier to follow. A reference may avoid an extra assignment in a narrow case, but readability and a clear function contract should matter more than a minor convenience.
Return Values Versus References
Choose a normal return for calculations
For calculations, formatting, and transformations, a normal return value usually makes the result explicit. The caller can compare the old and new values and decide whether to keep the change.
Use multiple outputs carefully
A reference parameter can provide an additional output, but an array or value object may communicate several results more clearly. When a function changes an input and returns a result, document both effects so callers do not miss the mutation.
References With Objects
Keep object behavior clear
Objects have their own assignment and mutation behavior in PHP, so beginners should not assume that object examples behave exactly like scalar variables. A method that changes an object’s property can already communicate mutation without a reference parameter.
Avoid unnecessary object references
Use a reference only when the object variable itself must be redirected or a specific API requires it. For normal object method calls, focus on the object’s public interface and keep mutation inside well-named methods.
Use Type Declarations
Make the contract visible
Type declarations can show what a reference parameter should contain, for example function addCount(int &$count). This does not remove the need for validation, but it makes incorrect calls easier to detect.
Combine types with clear names
Names such as $total, $items, or $counter explain the role of a mutable parameter. Avoid vague names and explain whether the function appends, replaces, normalizes, or removes data.
Common Mistakes
Adding an ampersand at the call
The reference marker belongs in the function definition. Adding it to an ordinary call is unnecessary and can make code harder to read. Check the definition to understand whether an argument may be modified.
Passing expressions
A reference requires a variable or another valid reference. Passing a literal, calculation, or function result as though it were a variable is invalid or produces a notice. Store a value in a variable first when a reference is genuinely required.
Forgetting side effects
A caller may reuse a variable after a function call and accidentally rely on its changed value. Document side effects and choose a return value when mutation is not essential.
Default Reference Parameters
Optional values need a clear rule
A reference parameter can have a default value, but the behavior should be obvious when the caller omits it. Decide whether the function should calculate a temporary value, skip the update, or return a separate status.
Test both call paths
Write one test that passes a variable and another that omits the optional argument. Check the output and any changes to the caller’s state. This prevents a default branch from behaving differently than expected.
When Should You Use a Reference?
Good use cases
- A procedure intentionally fills or updates a caller-owned collection.
- A function must return more than one output and the contract is documented.
- A counter or accumulator is shared across a controlled operation.
- An existing API specifically requires a reference parameter.
When to avoid it
Avoid references for simple calculations, formatting, or values that can be returned naturally. Hidden mutation makes testing and maintenance harder, especially when a function is called from several places.
Debugging Reference Behavior
Inspect before and after
Print or inspect the value before the call, inside the function, and after the call in a small development example. This shows whether the parameter was changed and whether the function received the variable you expected.
Reduce the example
Remove unrelated database, form, and template code while debugging. A small function with one variable makes reference behavior much easier to verify. Restore the larger application only after the isolated example works.
Testing Checklist
- Pass a scalar variable and confirm the expected mutation.
- Pass an array and verify keys and values after the call.
- Test a negative and zero numeric value.
- Try an invalid expression and confirm the error behavior.
- Test optional parameters with and without an argument.
- Check that a normal return value is not clearer.
- Document every intentional side effect.
FAQ
What is call by reference in PHP?
It allows a function parameter to refer to the caller’s variable. Changes made through that parameter can therefore change the original value outside the function.
How do I pass a variable by reference?
Place an ampersand before the parameter in the function definition, then pass a variable normally when calling the function.
What is the difference between pass by value and pass by reference?
Pass by value gives the function a value to work with, while pass by reference gives it access to the caller’s variable. A reference can create a side effect in the caller’s scope.
Can arrays be passed by reference in PHP?
Yes. An array can be declared as a reference parameter when the function intentionally needs to update the caller’s array. Returning a new array may be clearer for many transformations.
Should every PHP function use references?
No. Use references only when the mutation is necessary and documented. For ordinary calculations and transformations, a return value is usually easier to understand.
Conclusion
PHP call by reference lets a function modify a caller-owned variable through a reference parameter. The syntax is simple, but the side effect should always be deliberate and visible in the function’s contract.
Use the existing string and number examples to understand the behavior, then compare references with normal return values before choosing an implementation. Clear data flow is usually more valuable than avoiding one assignment.
Document Mutable Parameters
Explain the side effect
A short description above a reference parameter should say what changes and when. This is especially helpful when the function is used in validation, import, or service code where the same variable may be reused afterward.
Keep scope controlled
Pass a reference through a small, focused function rather than allowing many unrelated functions to share and mutate one variable. Narrow responsibilities make the final value easier to predict and test.
Final Review
References are a useful PHP feature when an intentional in-place update is part of the design. When a return value communicates the operation more clearly, prefer the return value and avoid hidden state changes.




Write a comment