Jump to content

Recommended Posts

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

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.