PHP Call By Value and By Reference

In this lesson, you will learn about the Call By Value and By Reference in PHP, their usage, and examples to better understand the topic.


PHP Call By Value

You can use PHP to call both the value and reference functions. When using PHP’s call-by-value method, the actual value won’t change if the parameter is altered inside the function. Let’s learn what calling by value means by using instances.

Syntax

function CallbyValueFunc($FirstParameter, $SecondParameter){
// Code to be processed here
}
CallbyValueFunc (“PassAnyString”,” PassAnyString”)

Example

<?php
echo "PHP ***Call by Value*** Function Example";
echo "<br>";
echo " ------------Example (1)------------------ ";
echo "<br>";
//concatenating string
function callbyvalueFunc($string2)

{
    $string2 .= 'Call By Value';
}
$string = 'Actual ';
callbyvalueFunc($string);
echo $string;
echo "<br>";
echo " ------------Example (2)------------------ ";
echo "<br>";
function incrementFunc($var)
{
    $var++;
}
$var = 10;
incrementFunc($var);
echo $var;
?>

Output

PHP ***Call by Value*** Function Example
------------Example (1)------------------
Actual
------------Example (2)------------------
10

In the above example 1, the $string variable is passed to the callbyvalueFunc function where the string ‘Call By Value ‘ is concatenated, which prints the outcomes of the $string variable ‘Actual. ‘ This is because only modifications are made in the local $string2 variable, which is not reflective of the varying $string. As in Example 2, the value of the increment remains at 10 even after calling incrementFunc.


PHP Call By Reference

In the case of a PHP call by reference, the actual value will change if it is modified inside the function. In this situation, you must include the & (ampersand) symbol with official statements. The reference to the variable is shown by the &.

Syntax

function CallbyRefFunc(&$FirstParameter, &$SecondParameter){
// Code to be processed here
}
CallbyRefFunc (“PassAnyString”,” PassAnyString”)

Using instances, let’s comprehend the notion of calling by reference.

Example

<?php
echo "PHP ***Call by Reference*** Function Example";
echo "<br>";

echo " ------------Example (1)------------------ ";
echo "<br>";
//concatenating string
function callbyvalueFunc(&$string2)
{
    $string2 .= 'Call By Reference';
}
$string = 'Actual ';
callbyvalueFunc($string);
echo $string;
echo "<br>";
echo " ------------Example (2)------------------ ";
echo "<br>";
function incrementFunc(&$var)
{
    $var++;
}
$var = 10;
incrementFunc($var);
echo $var;
?>

Output

PHP ***Call by Reference*** Function Example
------------Example (1)------------------
Actual Call By Reference
------------Example (2)------------------
11

In the first example, the string “Call By Reference” is concatenated using the $string variable passed to the callbyvalueFunc function. Additionally, it reflects how the variable $string now has a different value. Similarly, calling incrementFunc causes the increment value to increase by 11 afterward.

This concludes the PHP Call By Value and By Reference lesson. In the next lesson, you will learn about Default and Variable arguments in PHP.