mostafatalebi Posted June 5, 2013 Share Posted June 5, 2013 Hello is "passing by reference" which is indicated like &$variable, is used to reduce memory consumption? or it has other uses? Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/ Share on other sites More sharing options...
cpd Posted June 5, 2013 Share Posted June 5, 2013 (edited) No, the PHP Compiler will optimise your code. The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses" - http://www.php.net/manual/en/language.references.whatare.php Passing by reference allows you to directly manipulate content within a function/method of a variable defined outside the scope of the function/method. Edited June 5, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434207 Share on other sites More sharing options...
kicken Posted June 5, 2013 Share Posted June 5, 2013 It's used to allow a function the ability to modify a variable. One example is to return an error message from a function, such as how fsockopen does. Another example would be to directly modify an array, such as what sort and related functions do. Passing a variable by reference won't save any memory generally speaking. PHP won't copy a variable's value until it needs (due to a write operation) so if your function never makes a write statement to the parameter then the variable never gets copied and no extra memory is used. Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434209 Share on other sites More sharing options...
mostafatalebi Posted June 5, 2013 Author Share Posted June 5, 2013 I'm confused. It seems the php document is saying the same thing as I but in other words. I say it "reduces the memory consumption". Now read the below example of mine and correct me if I'm wrong: EXAMPLE: we have variable $x = "this is a string"; then I do &$s = $x; I think it means that, "this is a string" has one location in the memory, then two variables with two different names can access it(passing by reference). While: $x = "this is a string"; $s = $x; makes a copy of "this is a string" as variable $x content. Thus, logically, now it occupies two locations of the memory (regular variable assignment). Am I right? Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434224 Share on other sites More sharing options...
Jessica Posted June 5, 2013 Share Posted June 5, 2013 (edited) No, the PHP Compiler will optimise your code. The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses" - http://www.php.net/manual/en/language.references.whatare.php@mostafatalebi - Did you read this? Edited June 5, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434228 Share on other sites More sharing options...
mostafatalebi Posted June 5, 2013 Author Share Posted June 5, 2013 @mostafatalebi - Did you read this? I have. But did you pay attention to "no" by him? So he told that my first justification is not true. This added some confusion to me, therefore I ask my question again with some points added to get a complementary answer with regard to theirs. Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434230 Share on other sites More sharing options...
Jessica Posted June 5, 2013 Share Posted June 5, 2013 He said No as in they are not "used to reduce memory consumption". He backed that up by showing the manual which says "The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses" Your reply talked about memory addresses. Which it specifically says a reference IS NOT. You said "correct me if I'm wrong:" - You're wrong. Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434231 Share on other sites More sharing options...
cpd Posted June 5, 2013 Share Posted June 5, 2013 (edited) ...is used to reduce memory consumption? No. Arguably I should have been clearer as to which question I was answering. Thanks for clarifying Jessica. Edited June 5, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434232 Share on other sites More sharing options...
mostafatalebi Posted June 5, 2013 Author Share Posted June 5, 2013 then please explain this: according to this explanation: "The reference is merely a means of creating another variable for the same content": what is the difference between regular assignment ($x = $s) and passing by reference? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434235 Share on other sites More sharing options...
cpd Posted June 5, 2013 Share Posted June 5, 2013 (edited) Function/methods within PHP replicate variables passed to them via parameters therefore, you are manipulating a replica of the variable not the variable itself. When passing by reference, you are then manipulating the actual value itself even though its defined outside the scope of the method/function. E.g. function plusTwo($var) { $var+= 2; } $a = 1; plusTwo($a); echo $a; // Output is 1 still because $var is a replica of the value passed in. /* ------------- */ function plusTwo(&$var) { $var+= 2; } $a = 1; plusTwo($a); echo $a; // Output is 3 because you passed the variable in by reference and the value was directly manipulated With regards to assignments: $a = 2; $b = 4; $c =& $a; $d = $b; echo $a; // Output is 2 echo $b; // Output is 4 echo $c; // Output is 2 echo $d; // Output is 4 $c+= 1; $d+= 2; echo $a; // Output is 3 because $c directly manipulated the value it references to echo $b; // Output is still 4 echo $c; // Output is 3 echo $d; // Output is 6 because $d is a replica of $b and therefore has a different value Edited June 5, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434246 Share on other sites More sharing options...
mostafatalebi Posted June 5, 2013 Author Share Posted June 5, 2013 oh! very comprehensive definition the last one. thanks a lot. great. I needed such explanation. I was totally in another page when thinking about references. Thanks buddy Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434250 Share on other sites More sharing options...
kicken Posted June 5, 2013 Share Posted June 5, 2013 (edited) $x = "this is a string"; $s = $x; makes a copy of "this is a string" as variable $x content. Thus, logically, now it occupies two locations of the memory (regular variable assignment). Am I right? $s = $x will make what is known as a copy-on-write reference to $x. In other words both $x and $s point to the same value, just like if you had use a reference. The difference is that if you ever do anything that would cause either of them to change, eg $s=strtolower($s); then PHP will copy them at that point. So until you perform some sort of modification to either variable, they still only occupy a single instance of memory and there is no additional overhead copying values over. As such, using a reference merely to save memory/reduce overhead is unnecessary. Only use a reference if you have another reason for doing so. Edited June 5, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/278797-passing-by-reference/#findComment-1434296 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.