passing arguments by reference

You may want any changes made to an argument within a function to be reflected outside of the function's scope. Passing the argument by reference accomplishes this. Passing an argument by reference is done by appending an ampersand to the front of the argument.

Passing Arguments by Reference Syntax :

<?php
function functionname(&variable_a, variable_b)
{
 -------------
 -------------
 -------------
}
?>

Passing Arguments by Reference Example:


<?php
$cost = 20.99;
$tax = 0.0575;
function calculateCost(&$cost, $tax)
{
   // Modify the $cost variable
   $cost = $cost + ($cost * $tax);
   // Perform some random change to the $tax variable.
   $tax += 4;
}
calculateCost($cost, $tax);
printf("Tax is %01.2f%% <br />", $tax*100);
printf("Cost is: $%01.2f", $cost);
?>

Out put:

Tax is 5.75%;
Cost is $22.20;

Note : The value of $tax remains the same, although $cost has changed.





Content