PHP Call By Value

In this method, values of a variable is passed to a function. On passing a variable value to a function creates another address of the same passed variable. So, when passed parameter process withing function, it doesn’t affect contents of the actual parameter.


Let’s understand with an example –

<?php  
function increment($value){  
    $value++;  
    return $value;  
}  
$x = 10;  
$y = increment($x);  
echo $x; //Output: 10  
echo $y; //Output: 11
?>   

Let’s take another example of swapping two numbers-

<?php
function swap($x,$y)
{
    $c=$x;
    $x=$y;
    $y=$c;
}
$x1=5;
$x2=10;
echo "Before calling the function swap:"."<br>";
echo "The two values are: ".$x1." ".$x2."<br>";
swap($x,$y);
echo "After calling the function swap:"."<br>";
echo "The two values are: ".$x1." ".$x2."<br>";
?>

//result
Before calling the function swap:
The two values are: 5 10
After calling the function swap:
The two values are: 5 10

Trending Topics