More than thousands of built-in functions prove the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined function with example was explained in 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 programming. So, in this article I will discuss how to call PHP function by value and by reference with example

Function call by value in PHP
If any PHP function accepts parameters, we usually send arguments when 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 arguments 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 be reference technique, the value of the variable will be changed if the value is changed within function. 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 is being changed and we can find the changed value on 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 be changed if it as altered within function. 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.
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 contact me from Contact us page. I will try my best to stay with you.