Call by Value in PHP with Examples

In PHP, by default, call by value is the method used to pass arguments to functions. This means that when you pass a variable to a function, a copy of the variable’s value is passed, not the original. Therefore, any changes made to the argument inside the function do not affect the original variable.

Let’s break down call by value in PHP with easy-to-follow steps and examples.

Step 1: Understanding Call by Value
In call by value, the values of actual arguments (the ones passed during the function call) are copied to the formal arguments (the ones used inside the function). This way, the function works with its own copy of the variables, and any changes made inside the function will not affect the original variables outside the function.

Example 1: Basic Call by Value Example

<?php  
   function change_name($nm) {
      echo "Initially, the name is $nm \n";
      $nm = $nm . "_new";
      echo "This function changes the name to $nm \n";
   }

   $name = "John";
   echo "My name is $name \n";
   change_name($name);
   echo "My name is still $name";
?>

Output:

My name is John
Initially, the name is John
This function changes the name to John_new
My name is still John
Here, the function change_name() modifies the local variable $nm, but the original $name variable remains unchanged.

Step 2: How Call by Value Works
Actual Arguments: These are the values passed to the function, such as $name in the example above.
Formal Arguments: These are the variables in the function definition, like $nm in the example.
Since PHP uses call by value, the function change_name() works with a copy of $name, so changes made inside the function do not affect the original variable outside the function.

Step 3: Use Cases of Call by Value
Call by value is ideal when you need to perform computations or modify data inside a function without affecting the original values.

Example 2: Call by Value with Addition

<?php
   function addFunction($num1, $num2) {
      $sum = $num1 + $num2;
      return $sum;
   }

   $x = 10;
   $y = 20;
   $num = addFunction($x, $y);
   echo "Sum of the two numbers is: $num";
?>

Output:

Sum of the two numbers is: 30
Here, the function addFunction() calculates the sum of the two numbers without altering the original values of $x and $y.

Step 4: No Changes to Original Variable
In call by value, even if a function modifies the passed argument, the original variable remains unaffected.

Example 3: Incrementing a Value

<?php
   function increment($num) {
      echo "The initial value: $num \n";
      $num++;
      echo "This function increments the number by 1 to $num \n";
   }

   $x = 10;
   increment($x);
   echo "Number has not changed: $x";
?>

Output:

The initial value: 10
This function increments the number by 1 to 11
Number has not changed: 10
In this example, the function increment() increases the value of $num, but the original $x variable remains unchanged after the function call.

Step 5: Benefits of Call by Value
Safety: The original variables are not altered, which is useful when you want to ensure that data remains unchanged.
Isolation: Changes made inside the function are local, keeping the scope of modifications limited to the function.

Call by value is a key feature in PHP that ensures variables passed to a function are not altered outside the function’s scope. This is beneficial for computations or cases where you want to maintain the integrity of the original data. By understanding call by value, you can write more secure and predictable PHP code.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *