DarkMantis Posted February 24, 2012 Share Posted February 24, 2012 Hi all, I have been coding in PHP for a fair while now and I have come across variables by reference, but I don't really know: a) how they work; b) when to use them; c) why they are used; Can anyone here please clarify these issues please. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/257710-php-variables-by-reference/ Share on other sites More sharing options...
AyKay47 Posted February 24, 2012 Share Posted February 24, 2012 http://php.net/manual/en/language.references.php Quote Link to comment https://forums.phpfreaks.com/topic/257710-php-variables-by-reference/#findComment-1320827 Share on other sites More sharing options...
Zane Posted February 24, 2012 Share Posted February 24, 2012 You use references when you need to avoid the replication of variables. Typically, when you pass a variable to a function, it is within the scope of that function only, and you must return the desired variable to get what you want your function to do. When passing by reference, you are preserving the scope to that variable. It's much like when you order something with Fedex. When your package is received by Fedex, the don't go out and get a duplicate item to pass to the next branch. They send YOUR package throughout all of the branches until it reaches your door. If Fedex didn't "pass by reference", then your item would be received, put into a different box, perhaps even exchanged with a different yet similar item, and so on to each branch until it reached your door. Hopefully that puts things into perspective. Quote Link to comment https://forums.phpfreaks.com/topic/257710-php-variables-by-reference/#findComment-1320843 Share on other sites More sharing options...
DarkMantis Posted February 27, 2012 Author Share Posted February 27, 2012 That's a very good analogy. Okay I think I understand now. Thanks again for the clarification! All the best! Quote Link to comment https://forums.phpfreaks.com/topic/257710-php-variables-by-reference/#findComment-1321627 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.