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
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Dave Baldwin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
rgb192

>>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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
rgb192

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


thanks
Ray Paseur

&$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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes