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
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
When &$variable is passed to a function, that means that any operations on $variable are affecting the original variable.  When just $variable is passed to a function, only a copy of it is passed and operations on it inside the function do not affect the original variable.
0
Julian HansenCommented:
Consider the code below. This demonstrates the different ways of using value and reference parameters.

In the first instance the swap function is called passing parameters by value and after return from call parameters are unchanged

In the second call references to the variables are passed to the same function - on return the values are swapped.

In the third instance the second swap function (B - swap2) is called where parameters are declared as reference parameters.

As of PHP 5.3 and 5.4 call-time pass-by-reference is deprecated and you will receive a warning and an error in the two versions respectivley (refer http://php.net/manual/en/language.references.pass.php)
<?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;

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

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

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

Open in new window

0
Loganathan NatarajanLAMP DeveloperCommented:
As per this reference url, http://php.net/manual/en/functions.arguments.php

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.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

rgb192Author Commented:
>>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)?
0
Julian HansenCommented:
Part 4 is passing by reference - I am assuming that PHP sees this as a reference call as there are no pointer types in PHP the net result is the same as Part 3

Inside and outside

Variables declared and modified inside the function vs those that are passed in as parameters.

If variables are passed into the function by value they count as inside values because a copy is made of the values submitted and any operations performed on the parameters in the function are theferore made to the copies. These copies are destroyed when the function exits in other words
function swap($a, $b)
{
}
$x = 5;
$y = 6;
swap($x, $y);

Open in new window

In this case a copy of $x and $y is created for parameters $a and $b so any modifications made to $a and $b INSIDE the function swap do not affect the values of $x and $Y OUTSIDE the function. Likewise
function swap(&$a, &$b)
{
}
$x = 5;
$y = 6;
swap($x, $y);

Open in new window

In the above code a reference (pointer) to the values $x and $Y are passed to the function and any changes made to $a and $b INSIDE the function swap result in the values of $x and $y changing outside of the function.
0
Ray PaseurCommented:
The function definition is a code template.  The code is not altered.  Only the values of the variables can be altered.  The values of the variables that are not passed by reference are meaningful only inside the function; you can think of them as being discarded and renewed each time the function is called. (Exceptions: static, but don't worry about that now). The only way for a function to get data out to the larger scope of the script is to return the data.  A function can only return one variable, but the return variable can be an array or object.

However if you pass a variable by reference, the function is not operating on copies inside its own internal variables.   It's operating on the variables that are part of the larger scope.  It can change the values, the form or even unset() the variables.

If you write a function that uses the global keyword (an anti-practice in object oriented design) you can alter data outside of the function.  This violates the principle of encapsulation which says, roughly, that code should not modify the data that is given to it, but instead create new values that can be returned.  When you pass variables by reference, you get something of the same effect.  When you assign two variables to the same object, you get an additional point of confusion.  These things don't matter very much if you're working by yourself because you only have to keep one person's work organized.  When you start working with other programmers, especially other programmers who are not native English speakers, you will find that encapsulation, interfaces, traits and things like that become very important.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
so &$ is actual variable and $ is just a copy which does not affect original variable


thanks
0
Ray PaseurCommented:
&$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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.