Table of contents
1.
Introduction
2.
Call By Value
2.1.
Example of Call By Value
3.
Call by Reference
3.1.
Example of Call By Reference
4.
When to Use Call by Value and Call by Reference in PHP?  
4.1.
When to Use Call by Value  
4.2.
When to Use Call by Reference  
5.
Differences Between Call By Value and Call By Reference
6.
Frequently Asked Questions
6.1.
When should I use call by value in PHP?
6.2.
What is the advantage of using call by reference?
6.3.
How can I prevent a function from modifying my original variable?
7.
Conclusion
Last Updated: Feb 15, 2025
Medium

Call by Value and Call by Reference in PHP

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In PHP, functions can pass arguments using Call by Value or Call by Reference. In Call by Value, a copy of the variable is passed, so changes inside the function do not affect the original variable. In Call by Reference, the actual variable is passed using the & symbol, allowing the function to modify its value.

Call by Value and Call by Reference in PHP

In this article, you will learn the differences between Call by Value and Call by Reference in  PHP with examples to understand their usage.

Call By Value

Call by value is the default way of passing arguments to a function in  PHP. When a variable is passed using call by value, a copy of the variable’s value is sent to the function. This means that any changes made to the variable inside the function do not affect the original variable.

Example of Call By Value

<?php
function increaseValue($num) {
    $num += 10; // Increase the value by 10
    echo "Value inside function: $num\n";
}

$originalValue = 20;
increaseValue($originalValue);
echo "Original value after function call: $originalValue\n";
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output:

Value inside function: 30
Original value after function call: 20

 

Explanation:

  • The function increaseValue takes a parameter $num.
     
  • The value of $num is increased by 10 inside the function.
     
  • However, the original variable $originalValue remains unchanged because only a copy was modified inside the function.

Call by Reference

In call by reference, instead of passing a copy of the variable, the actual memory address of the variable is passed. This means that any changes made inside the function directly affect the original variable.

To pass a variable by reference, we use the ampersand (&) before the parameter name in the function definition.

Example of Call By Reference

<?php
function increaseValueByReference(&$num) {
    $num += 10; // Increase the value by 10
    echo "Value inside function: $num\n";
}
$originalValue = 20;
increaseValueByReference($originalValue);
echo "Original value after function call: $originalValue\n";
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

Value inside function: 30
Original value after function call: 30

 

Explanation:

  • The function increaseValueByReference takes a parameter by reference using &$num.
     
  • The value of $num is increased by 10 inside the function.
     
  • Since the variable is passed by reference, changes made inside the function affect the original variable $originalValue.

When to Use Call by Value and Call by Reference in PHP?  

Choosing between call by value & call by reference depends on the specific requirements of your program. Let’s discuss when to use each method:  

When to Use Call by Value  

Call by value is the default behavior in  PHP, & it’s ideal in the following situations:  

1. Preserving Original Data: If you want to ensure that the original data remains unchanged, use call by value. This is useful when you’re working with sensitive or critical data that should not be modified accidentally.  
 

2. Working with Simple Data Types: For simple data types like integers, floats, or strings, call by value is often sufficient. Since these data types are small in size, creating a copy doesn’t significantly impact performance.  
 

3. Avoiding Side Effects: Call by value prevents unintended side effects because changes inside the function don’t affect the original variable. This makes your code more predictable & easier to debug.  

When to Use Call by Reference  

Call by reference is useful in specific scenarios where you need to modify the original data directly. Let’s take a look at a few common use cases:  

1. Modifying Original Data: If your function needs to update the original variable, use call by reference. This is common in functions that perform operations like sorting, filtering, or updating values.  
 

2. Working with Large Data Structures: For large data structures like arrays or objects, call by reference can improve performance. Instead of creating a copy of the entire data structure, PHP works directly with the original, saving memory & processing time.  
 

3. Returning Multiple Values:  PHP functions can only return one value directly. If you need to return multiple values, you can use call by reference to modify multiple variables passed to the function.  

Differences Between Call By Value and Call By Reference

ParametersCall By ValueCall By Reference
Passing MethodPasses a copy of the variablePasses the actual variable reference
Effect on Original VariableNo changeChanges reflect in the original variable
Memory UsageUses more memory (creates a copy)Uses less memory (no duplication)
PerformanceSlower for large data structuresFaster for large data structures
Use CaseWhen data should not be modifiedWhen changes should persist outside the function

Frequently Asked Questions

When should I use call by value in PHP?

Use call by value when you do not want the original variable to be modified by the function.

What is the advantage of using call by reference?

Call by reference saves memory and allows functions to modify variables directly, making it useful for handling large datasets.

How can I prevent a function from modifying my original variable?

Use call by value (default method) to ensure that a function receives a copy of the variable, keeping the original data intact.

Conclusion

In this article, we explored Call by Value and Call by Reference in  PHP and how they affect function arguments. Call by Value passes a copy of the variable, keeping the original unchanged, while Call by Reference passes the actual variable, allowing modifications. Understanding these concepts helps in writing efficient and optimized PHP code with better memory management.

Live masterclass