Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

reference of a variable passed to function / method

if a reference of a $variable (&$variable) is passed to a function / method

does this mean that the $variable's value will change and the function / method value will change
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

>>When &$variable is passed to a function, that means that any operations on $variable are affecting the original variable.

does that mean that both function and $variable are altered?






<?php
// A) Parameters passed by value
function swap($a, $b)
{
    $temp = $a;
    $a = $b;
    $b = $temp;
}
// B) Parameters passed by reference
function swap2(&$a, &$b)
{
    $temp = $a;
    $a = $b;
    $b = $temp;
}

$x = 5;
$y = 6;

echo'// 1. Swap using value<br>';
swap($x, $y);
echo "X: $x<br/>";
echo "Y: $y<br/>";

echo'// 2. Swap using call-time pass-by-reference (Deprecated as of PHP 5.3)<br>';
swap(&$x, &$y);
echo "X: $x<br/>";
echo "Y: $y<br/>";

echo'// 3. Swap using reference parameters<br>';
swap2($x, $y);
echo "X: $x<br/>";
echo "Y: $y<br/>";

echo'//4. Swap using reference of reference parameters<br>';
swap2(&$x, &$y);
echo "X: $x<br/>";
echo "Y: $y<br/>";
?>

Open in new window


output:
// 1. Swap using value
X: 5
Y: 6
// 2. Swap using call-time pass-by-reference (Deprecated as of PHP 5.3)
X: 6
Y: 5
// 3. Swap using reference parameters
X: 5
Y: 6
//4. Swap using reference of reference parameters
X: 6
Y: 5


what does part 4 do?
echo'//4. Swap using reference of reference parameters<br>';
swap2(&$x, &$y);
echo "X: $x<br/>";
echo "Y: $y<br/>";


>>By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

So which one is inside, outside, (inside and outside)?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

so &$ is actual variable and $ is just a copy which does not affect original variable


thanks
&$x is the pointer to the data that is contained in $x.  A pointer is the information that is in the symbol table.  The data is the content of the variable that is pointed to by the symbol table.