SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Usage of =&

  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Dallas
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Usage of =&

    I frequently see this used throughout a lot of PHP scripts but I can't figure out what it's used for. When trying to search for it on Google or forums it doesn't return any results due most likely to the restriction of certain characters.. can anyone tell me what this is or point me to a resource where I can learn more about it?

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/languag...ors.precedence

    theres an &= Operater, which is an binary AND and an assignment, i guess something like +=, but with binary operations i'm not that experienced

  3. #3
    SitePoint Wizard Dangermouse's Avatar
    Join Date
    Oct 2003
    Posts
    1,024
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    &= is different to =&. The & means it creates a reference of whatever is being assigned. I believe $foo = &$bar and $foo =& $bar are equal, but i prefer the first syntax.

  4. #4
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Dallas
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick info..

    I've looked at that link pertaining to references and here is what I get from that and the Zend documentation on references:

    - It allows a variable to be referenced by another variable, a change in one causes the other to change as well
    - It increases performance, although from reading the Zend article http://www.zend.com/zend/art/ref-count.php it seems that referencing is done automatically for you by PHP4.. they both take up the same memory spot and it's released when no longer referenced to free memory usage
    - The most savings are when you work with large arrays containing lots of information, you don't make a copy of the information when you do something like:

    $oldvar = // very large array
    $newvar = $oldvar;

    - I still do not understand why you would reference a function like

    function &bar ()
    {
    // code here
    }

    or why references are needed using &= if PHP4 automatically handles it for you?

    Maybe references are too advanced for a 6 month PHP programmer but I do notice a lot of professional PHP scripters using them and I want to get into the habit of good coding.

  5. #5
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP 4 does NOT auto-create references.

    When you do something like this....

    $myObject = new ObjectThing();

    in PHP4, PHP first creates the object, then creates $myObject as a copy of the object. Thus, you have 2 objects floating around, and no way of accessing one of them.

    If you do...

    $myObject = &new ObjectThing();

    instead, you are first creating the object, and then creating $myObject as a reference to the original object. Thus, you now only have one object, and you have a means of communicating with that object.

    I have seen people use the same technique in PHP 5, but as far as I'm aware, it's counter-intuitive since PHP 5 automatically creates references.

  6. #6
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    argh, i knew that, but i've alwaays a space between = and &, and the last few days were dedicated to php5, how fast one can forget php4 stuff

    Yes, php passes Objects by reference as "default".

    and sth. like function &foo() { return $retvalue; }
    returns $retvalue as a reference and not as a copy

  7. #7
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Dallas
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So PHP5 automatically creates references for you but PHP4 doesn't.. does that mean if you're using PHP5 then you don't need to be concerned with this type of coding for optimization and efficiency?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •