PHP Call by Value : A Tutorial with Examples

In PHP, when a function is called and arguments are passed, by default, PHP uses call by value. This means that the function receives a copy of the variable, and any modifications to the parameter inside the function do not affect the original variable.

Understanding this behavior is crucial for avoiding unintended side effects in your code.

In this tutorial, we will cover:

Let’s explore each of these topics with examples and explanations.

1. What is Call by Value?

Call by value is a method of passing arguments to a function in which the value of the argument is passed, rather than the variable itself. The function operates on a copy of the argument’s value, and changes made to the parameter inside the function do not affect the original variable outside the function.

In PHP, function parameters are passed by value by default, meaning that a copy of the variable’s value is made when calling the function.

2. How Call by Value Works in PHP

When a variable is passed by value to a function, PHP creates a copy of the variable’s value and passes it to the function. Any changes made to the parameter inside the function affect only the local copy, not the original variable.

Example (Call by Value in PHP):

<?php
// Function to increment a value by 10
function addTen($number) {
    $number += 10;
    echo "Inside function: $number\n";
}

// Original variable
$value = 20;

// Call the function with the variable
addTen($value);

// Print the original variable after the function call
echo "Outside function: $value\n";
?>

Output:

Inside function: 30
Outside function: 20
  • In this example, the function addTen() modifies the local copy of the parameter $number. Even though $number is changed inside the function, the original variable $value outside the function remains unchanged because of call by value.

3. Examples of Call by Value

Let’s look at more examples that illustrate how call by value works with different data types.

Example (Call by Value with Strings):

<?php
// Function to append a string
function appendText($str) {
    $str .= " is awesome!";
    echo "Inside function: $str\n";
}

// Original variable
$text = "PHP";

// Call the function with the string variable
appendText($text);

// Print the original variable after the function call
echo "Outside function: $text\n";
?>

Output:

Inside function: PHP is awesome!
Outside function: PHP
  • In this example, the string $text is passed by value to the function appendText(). The original string remains unchanged outside the function.

Example (Call by Value with Arrays):

<?php
// Function to modify an array
function modifyArray($arr) {
    $arr[] = "New Item";
    print_r($arr);
}

// Original array
$array = ["Item 1", "Item 2"];

// Call the function with the array
modifyArray($array);

// Print the original array after the function call
print_r($array);
?>

Output:

Array
(
    [0] => Item 1
    [1] => Item 2
    [2] => New Item
)
Array
(
    [0] => Item 1
    [1] => Item 2
)
  • In this example, the array $array is passed by value to the function modifyArray(). The function modifies the local copy of the array, but the original array outside the function remains unchanged.

4. Impact of Modifying Parameters in Call by Value

Since PHP uses call by value by default, any modifications to the parameters inside the function do not affect the original values outside the function. The function operates on a copy of the values passed.

Example (Modifying Parameters in Call by Value):

<?php
// Function to square a number
function square($num) {
    $num *= $num;
    echo "Inside function (squared value): $num\n";
}

// Original variable
$originalNum = 5;

// Call the function
square($originalNum);

// Print the original variable after the function call
echo "Outside function (original value): $originalNum\n";
?>

Output:

Inside function (squared value): 25
Outside function (original value): 5
  • In this example, the square() function modifies the local copy of $num inside the function. The original variable $originalNum is unaffected by the function’s operations.

5. Difference Between Call by Value and Call by Reference

While call by value passes a copy of the argument to the function, call by reference passes a reference to the original variable. This means that changes made to the parameter inside the function will affect the original variable outside the function.

Call by Value vs. Call by Reference:

Call by Value Call by Reference
A copy of the variable’s value is passed to the function. A reference to the original variable is passed to the function.
Changes to the parameter do not affect the original variable. Changes to the parameter do affect the original variable.
Default behavior in PHP. Achieved using the & operator.

Example (Call by Reference):

<?php
// Function to add 10 by reference
function addTenByReference(&$number) {
    $number += 10;
    echo "Inside function: $number\n";
}

// Original variable
$value = 20;

// Call the function with the variable by reference
addTenByReference($value);

// Print the original variable after the function call
echo "Outside function: $value\n";
?>

Output:

Inside function: 30
Outside function: 30
  • In this example, the function addTenByReference() uses the & operator to modify the original variable $value. As a result, the original value is updated after the function call.

Summary of PHP Call by Value:

Concept Description
Call by Value The default method of passing arguments in PHP. A copy of the variable’s value is passed to the function.
Call by Value Behavior Modifying the parameter inside the function does not affect the original variable.
Impact of Modifications Any changes made inside the function affect only the local copy of the parameter.
Difference from Call by Reference Call by reference passes the actual variable, allowing changes to affect the original value.

Conclusion

In PHP, call by value is the default behavior when passing arguments to a function.

This means that functions operate on copies of the values, leaving the original variables unchanged. In this tutorial, we covered:

  • What call by value is and how it works in PHP.
  • Examples of call by value with different data types such as integers, strings, and arrays.
  • The impact of modifying parameters inside a function when using call by value.
  • The difference between call by value and call by reference.

Related posts

PHP Static Methods: A Tutorial with Examples

PHP Deleting Files: A Tutorial with Examples

PHP Copying Files: A Tutorial with Examples