๐Ÿงฎ PHP Functions
Estimated reading: 3 minutes 279 views

PHP Call by Value vs. Call by Reference โ€“ Function Argument Passing Explained

Understand the difference between passing arguments by value and by reference in PHP, and when to use each approach effectively.


Introduction โ€“ Why Call Types Matter in PHP

In PHP, when you pass a variable to a function, it can be done in two ways:

  • Call by Value โ€“ the function receives a copy of the variable.
  • Call by Reference โ€“ the function receives a reference to the original variable.

Choosing the right method helps control whether or not your function modifies the original data.

In this guide, youโ€™ll learn:

  • How call by value and call by reference work in PHP
  • Syntax for each type
  • Real-world examples and use cases
  • Best practices to avoid unexpected side effects

Call by Value โ€“ Default Behavior

In this mode, the function gets a copy of the argument, and changes do not affect the original variable.

function update($x) {
    $x = $x + 10;
    echo "Inside function: $x <br>";
}

$num = 5;
update($num);

echo "Outside function: $num"; // 5

Output:

Inside function: 15
Outside function: 5

$num remains unchanged because PHP uses call by value by default.


Call by Reference โ€“ Modify the Original Variable

Use & to pass a reference instead of a copy.

function modify(&$x) {
    $x = $x + 10;
    echo "Inside function: $x <br>";
}

$num = 5;
modify($num);

echo "Outside function: $num"; // 15

Output:

Inside function: 15
Outside function: 15

$num is modified both inside and outside the function.


Real-World Example โ€“ Swap Two Numbers

function swap(&$a, &$b) {
    $temp = $a;
    $a = $b;
    $b = $temp;
}

$x = 10;
$y = 20;

swap($x, $y);

echo "x: $x, y: $y"; // x: 20, y: 10

Use references when you want the function to change external data.


Reference Return from Function

function &getRef(&$value) {
    return $value;
}

$a = 50;
$ref = &getRef($a);
$ref = 100;

echo $a; // 100

The return is a reference to a variable, not just the value.


Best Practices

  • Use call by value when you want to preserve original data
  • Use call by reference when a function must update external state
  • Donโ€™t overuse references โ€” they can make code harder to debug
  • Document functions clearly if they modify arguments

Summary โ€“ Recap & Next Steps

Knowing when to pass by value vs by reference helps you control side effects and build more predictable functions.

Key Takeaways:

  • PHP passes variables by value by default
  • Use & to pass by reference and modify the original variable
  • Reference passing is great for mutating state like swapping values

Real-World Use Cases:
In-place sorting, modifying external variables, efficient memory usage in large datasets.


Frequently Asked Questions (FAQs)

Which is faster: call by value or call by reference?
Reference is slightly faster for large data structures, but it comes with side effects.

When should I use call by reference?
When you want the function to modify the original variable.

Is reference the default in PHP?
No. All arguments are passed by value unless specified with &.

Can I return a reference from a function?
Yes, use function &name() to return a reference.

Can I pass an array by reference?
Yes. Prefix the parameter with &: function update(&$arr) {}


Share Now :
Share

โžก๏ธ PHP Call by Value / Reference

Or Copy Link

CONTENTS
Scroll to Top