rgb192
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
does this mean that the $variable's value will change and the function / method value will change
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so &$ is actual variable and $ is just a copy which does not affect original variable
thanks
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.
ASKER
does that mean that both function and $variable are altered?
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)?