sKunKbad Posted May 27, 2009 Share Posted May 27, 2009 I've been using php for about 3 years, and have never come across a script that used =& like the following example: $foo =& new StdClass() What does this do? How is it different than just plain = Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/ Share on other sites More sharing options...
anupamsaha Posted May 27, 2009 Share Posted May 27, 2009 I've been using php for about 3 years, and have never come across a script that used =& like the following example: $foo =& new StdClass() What does this do? How is it different than just plain = Its PHP 4 style. Not required in PHP5. In PHP5, call by reference is a default. You can omit the "&" sign. Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843366 Share on other sites More sharing options...
sKunKbad Posted May 27, 2009 Author Share Posted May 27, 2009 Its PHP 4 style. Not required in PHP5. In PHP5, call by reference is a default. You can omit the "&" sign. OK, I have no sites using php4 anyway, but what was the reason to use =& anyways? I saw the docs on "return by reference", but I don't understand. Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843383 Share on other sites More sharing options...
KevinM1 Posted May 27, 2009 Share Posted May 27, 2009 Its PHP 4 style. Not required in PHP5. In PHP5, call by reference is a default. You can omit the "&" sign. OK, I have no sites using php4 anyway, but what was the reason to use =& anyways? I saw the docs on "return by reference", but I don't understand. Normal assignment is often achieved with 'return by value.' This means that a copy of the value is returned and stored in the variable being assigned to. 'Return by reference' actually stores a reference to that particular value/object/whatever. So, instead of having a copy, you're essentially dealing with the real thing. More info: http://www.adp-gmbh.ch/php/pass_by_reference.html , http://us2.php.net/references Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843422 Share on other sites More sharing options...
MadTechie Posted May 27, 2009 Share Posted May 27, 2009 okay i started to type this then i had a long phone call Its the same idea as a pointer in C. okey heres a function <?php $data = "123"; $data = addstuff($data); function addstuff($add) { $add = $add."Blar" return $add; } ?> Okay what that does is pass 123 and then returns 123456 Now the memory used is $data = "123"; then $data = "123"; $add= "123"; then $data = "123"; $add= "123Blar"; then $data = "123Blar"; okay.. now with Passing by Reference (not default in php 5) <?php $data = "123"; addstuff($data); function addstuff(&$add) { $add = $add."Blar" } ?> this is how it goes $data = "123"; then $data = "123"; and $add= $data; these take up the same memory block so if $add changes then $data changed then $add= "123Blar"; //so $data = 123Blar Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843436 Share on other sites More sharing options...
sKunKbad Posted May 28, 2009 Author Share Posted May 28, 2009 MadTechie, thanks for the explanation. I understand now. Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843766 Share on other sites More sharing options...
MadTechie Posted May 28, 2009 Share Posted May 28, 2009 Welcome, as a note since PHP5 there is a little change, from what i said.. Now the memory used is $data = "123"; then $data = "123"; $add= "123"; <---In PHP4 this is true BUT in PHP5 this is still a referance BUT as soon as it changes it takes a new memory block, where as pass by ref will not create a new block then $data = "123"; $add= "123Blar"; then $data = "123Blar"; Also please note that the function doesn't return anything and nothing gets set from the function, If the topic is solved please click solved Link to comment https://forums.phpfreaks.com/topic/159895-solved-why/#findComment-843771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.