onlyican Posted August 8, 2007 Share Posted August 8, 2007 Hey people I know what &$ does, it allows you to pass a variable into a function which will update the varibale But please tell me which method is correct or best Option 1: Add & when calling the function <?php function MyFunction($MyVar){ $MyVar = "How are You"; } $MyVar = "Hello"; MyFunction(&$MyVar); echo $MyVar; ?> Option 2: Add & when building function <?php function MyFunction(&$MyVar){ $MyVar = "How are You"; } $MyVar = "Hello"; MyFunction($MyVar); echo $MyVar; ?> Option 3: Add & in both <?php function MyFunction(&$MyVar){ $MyVar = "How are You"; } $MyVar = "Hello"; MyFunction(&$MyVar); echo $MyVar; ?> As you can guess, I want the result to be "How are You"; not "Hello"; //Edit, Added php tags Quote Link to comment https://forums.phpfreaks.com/topic/63930-clarify-use-of/ Share on other sites More sharing options...
GingerRobot Posted August 8, 2007 Share Posted August 8, 2007 As far as im aware, option 2 & 3 are the same. The only differant behaviour with option one is that it allows you to not always pass by reference. Wether or not there would be a time that having a function which sometimes passes by reference and sometimes by value would be useful/required, im not sure. Quote Link to comment https://forums.phpfreaks.com/topic/63930-clarify-use-of/#findComment-318641 Share on other sites More sharing options...
lemmin Posted August 8, 2007 Share Posted August 8, 2007 The & symbol indicates the address of the variable. Option 1 is the best. For option 1 you are simply passing the address of a variable to a (or any) function. For option 2, not mater what variable you use, it will always reference the address of it, making it difficult to keep the variable scopes in order. For option 3, I am actually surprised that it works. If php were a lower level language, I would expect that to change the address of the variable to something completely wrong, rendering it pretty much useless. Since it is PHP, I am would guess that it ignores the second &, making option 3 the same as 2. Quote Link to comment https://forums.phpfreaks.com/topic/63930-clarify-use-of/#findComment-318644 Share on other sites More sharing options...
Barand Posted August 8, 2007 Share Posted August 8, 2007 From the manual In recent versions of PHP you will get a warning saying that "Call-time pass-by-reference" is deprecated when you use a & in foo(&$a);. Quote Link to comment https://forums.phpfreaks.com/topic/63930-clarify-use-of/#findComment-318686 Share on other sites More sharing options...
onlyican Posted August 8, 2007 Author Share Posted August 8, 2007 So Option 2 is correct then. As it is always the case, you just add it to the function insetad of when calling the function Quote Link to comment https://forums.phpfreaks.com/topic/63930-clarify-use-of/#findComment-318755 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.