colin-java Posted July 21, 2010 Share Posted July 21, 2010 Hi, I understand passing by reference eg.. function f(&$param) { ... } But I can't properly grasp returning by reference when the & is before the function. Does it have anything to do with this: $a = 1; $b = &$a; So now $a and $b basically reference the same value. PS, I have not got to classes yet, so please don't use them in any explanations. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/ Share on other sites More sharing options...
trq Posted July 21, 2010 Share Posted July 21, 2010 You just explained it yourself. Both $a and $b reference the same value. Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/#findComment-1088906 Share on other sites More sharing options...
colin-java Posted July 21, 2010 Author Share Posted July 21, 2010 So what about function &func(){}, how does that work? Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/#findComment-1089077 Share on other sites More sharing options...
Zane Posted July 21, 2010 Share Posted July 21, 2010 if you give a function an &$variable, then whatever is done to that variable within the function is relegated back to the variable you put as the argument. For instance function addOne(&$a) { $a += 1; } $b = 1; echo $b; // Prints 1 addOne($b); echo $b; // Prints 2 Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/#findComment-1089086 Share on other sites More sharing options...
colin-java Posted July 21, 2010 Author Share Posted July 21, 2010 Thanks, I get that, but thats not what I'm asking... If I have a function: function &func($a) { .... } Whats going on here? I think I have to return something, I'm not sure. Is it somehow related to this.. ############################## $a = 1; $b = &$a; // so now $a and $b reference the same variable ############################## Thanks again for any help Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/#findComment-1089139 Share on other sites More sharing options...
KevinM1 Posted July 21, 2010 Share Posted July 21, 2010 Thanks, I get that, but thats not what I'm asking... If I have a function: function &func($a) { .... } Whats going on here? I think I have to return something, I'm not sure. Is it somehow related to this.. ############################## $a = 1; $b = &$a; // so now $a and $b reference the same variable ############################## Thanks again for any help See: http://php.net/manual/en/language.references.return.php Quote Link to comment https://forums.phpfreaks.com/topic/208367-returning-by-reference/#findComment-1089145 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.