Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined function in PHP was explained in the previous article. I also explained how to use variables inside a PHP function properly. Function call by value and call by reference is another important terms in PHP function. So, in this article I will discuss how to call PHP function by value and by reference with example.

Function Call by Value and Call by Reference
Function Call by Value and Call by Reference

Function call by value in PHP

If any PHP function accepts parameters, we usually send arguments when the function is called. If we send values as arguments when function is called, it is known as function call by value. With function call by value technique, the value of a variable that is sent as argument keeps unchanged although it is altered in function body.

We usually use function call value technique most of our PHP programs. The following code is an example of function call by value in PHP.

<?php
function addTen($num){
  $num=$num+10;
  print("Inside function:".$num);
}
$num=10;
addTen($num);
print("Outside function:".$num);
?>

Output

Inside function: 20

Outside function: 10.

In the above program, the addTen() function is being called with a variable named $num. After calling the function, the variable is being printed but the variable value is keeping unchanged because we are calling the function just value.

It is possible to change the variable value within a function. This technique is known as function call by reference. In the following section we will learn how to call PHP function by reference.

Function call by reference in PHP

Calling PHP function with the address of a variable instead of value is known as function call by reference. In function call by reference technique, the value of the variable will change if the value is changed within the function body.

The following program is an example of function call by reference in PHP.

<?php
function addTen($num){
   $num=$num+10;
   print("Inside function:".$num);
}
$num=10;
addTen(&$num);
print("Outside function: ".$num);
?>

Output

Inside function: 20

Outside function: 20

In the above program, we are calling the addTen() function with the address of a variable. Here the ampersand (&) sign indicates the address of a variable. After calling and executing the function, we can see that the value of the variable has changed and we can find the changed value on the output. 

In the above program, we are calling function by reference. But it is also possible to declare a PHP function as call by reference format. So, whenever the function will be called with any variable, the address of that variable will be used as parameter and the value of the variable will change if it is altered within the function body.

The following program is an example of declaring a PHP function as call by reference format.

<?php
function addTen(&$num){
   $num=$num+10;
   print("Inside function:".$num);
}
$num=10;
addTen($num);
print("Outside function:".$num);
?>

Output

Inside function: 20.

Outside function: 20

Here, the addTen() function has been declared as call by reference format. As a result, the value of the variable is being changed after executing the function body.

PHP function call by value and call by reference has been explained in this article. I hope you have now clear idea on PHP function call by value and call by reference.  However, if you face any problem to understand these topics, feel free to discuss in commnet or contact me from Contact us page. I will try my best to stay with you. 

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

PHP Delete Array Element with Built-in Function

Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array…

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

PHP Function Explanation

Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves the power of PHP language. Besides…

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.