jGiblets Posted March 20, 2012 Share Posted March 20, 2012 Hi all, I'm trying to understand passing by reference. Here is a copy of the code and the results: <?php $a1 = 15; $b1 = 20; echo addone($a1, $b1); echo "<br/>"; function addone($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; }; echo addonetwo($a1, $b1); function addonetwo($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; } ?> The result output is: 17 22 17 22 If I change the code to add "&" before the "addone" function: function addone(&$n1, &$n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; }; Then the output is: 17 22 19 24 I don't understand what's going on. Why is the "&" incrementing the changed variable and in the first example it's incrementing the variables as defined. Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/ Share on other sites More sharing options...
smerny Posted March 20, 2012 Share Posted March 20, 2012 think about it this way... when you don't pass by reference: $a1 ----> [15] $n1 ----> [15] if you change the [15] for $n1, it just changes that because nothing else is pointing to that [15] when you pass by reference it's like this: $a1 -----> [15] $n1 ---------^ so when you change the [15] for $n1, it also changes for $a1 Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/#findComment-1329278 Share on other sites More sharing options...
smerny Posted March 20, 2012 Share Posted March 20, 2012 in your example using references (added comments): <?php $a1 = 15; $b1 = 20; //outputs the 15+2 and the 20+2, also now $a1 and $b1 become 17 and 22 echo addone($a1, $b1); echo "<br/>"; function addone(&$n1, &$n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; }; //outputs the 17+2 and the 22+2, $a1 and $b1 remain unchanged at 17 and 22 echo addonetwo($a1, $b1); function addonetwo($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/#findComment-1329279 Share on other sites More sharing options...
jGiblets Posted March 20, 2012 Author Share Posted March 20, 2012 Ok, I think I'm following you. Adding the "&" reference is changing the $a1 and $b1 variables. This made a lot more sense when I put the reference marker on the "addonetwo" function and then added a third statement that incremented the returned output. $a1 = 15; $b1 = 20; echo addone($a1, $b1); echo "<br/>"; function addone($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; }; echo addonetwo($a1, $b1); echo "<br/>"; function addonetwo(&$n1, &$n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; } function addonetwothree($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; } echo addonetwothree($a1, $b1); ?> Then the output becomes: 17 22 17 22 19 24 You all are great! Thank you for the help. One related question. Let's say this was a longer more complicated function with other increments, how would I call back to the original $variables even after they had been incremented and changed. Let's say I wanted to add a "addonetwothreefour" function: function addonetwothreefour($n1, $n2){ $n1 = $n1 += 2; $n2 = $n2 += 2; return $n1 . " " . $n2; } and have it return: 17 22 Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/#findComment-1329282 Share on other sites More sharing options...
jGiblets Posted March 20, 2012 Author Share Posted March 20, 2012 Never mind, after sleeping on it, I think I can answer my own question. There is no way to call the original values after the have been passed by reference because the $variable has been assigned a new value and only exists in that form. Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/#findComment-1329418 Share on other sites More sharing options...
dragon_sa Posted March 20, 2012 Share Posted March 20, 2012 unless you declare them as another variable first, then you can go back to the untouched variables. Quote Link to comment https://forums.phpfreaks.com/topic/259311-trying-to-understand-passing-by-reference/#findComment-1329444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.