Google News
logo
PHP Program to Passing arguments by reference
In the following example of PHP program that demonstrates passing arguments by reference :
Program :
<?php

// Declare a function that modifies its argument by reference
function addTen(&$num) {
    $num += 10;
}

// Initialize a variable
$num = 5;

// Call the function and pass the variable by reference
addTen($num);

// Print the modified value
echo "The value of num after adding 10 is: $num";

?>
Output :
The value of num after adding 10 is: 15
In this example, we have declared a function `addTen` that takes one parameter `$num` by reference using the `&` operator. Inside the function, we add 10 to the value of `$num`.

Since `$num` is passed by reference, any changes made to it inside the function will also be reflected outside the function.

We then initialize a variable `$num` with the value `5`. We call the function `addTen` and pass the variable `$num` by reference.

Finally, we print out the modified value of `$num` using the `echo` statement. The output shows that the value of `$num` has been modified to `15` after adding 10 inside the function.