SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: =&

  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    =&

    Hi

    Really simple question but I can't find the answer.

    I'm reading through some PHP code, and there are a few lines with "=&". eg
    $db =& $this->GetDb();

    I've never seen this before and I can't find an explanation anywhere. Can anyone explain what the '&' does?

    Many thanks
    Jo

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you using php5 you should just replace these '=&' with '=' and don't worry about it. It's an old php4 style of passing objects by references, in php5 objects are passed by reference by default, so this style is no longer needed, it will even generate a notice.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It does have its place though.

    Basically it's a pass by reference - ever seen pointers?

    Example:
    PHP Code:
    $a 1;
    $b $a;
    $b++;
    /* $a = 1, $b = 2 */
    $a 1;
    $b =& $a;
    $b++;
    /* $a = 2, $b = 2 */ 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Basically it's a pass by reference - ever seen pointers?
    References are not pointers! Stop repeating this myth . They are two different concepts.

    Pointer is a data type (unavailable in PHP) which allows to access another piece of data by its address in memory.

    Reference (in PHP) is another name for the same variable.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the heads up!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all.

    So it looks like for my purposes I can just read it as "=" for now. Phew.

    Thanks for helping.
    Jo

  7. #7
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can be a tough thing to find information on because it's just symbols and search engines typically work using keywords.

    Here's the manual page you're looking for,
    http://us2.php.net/manual/en/language.references.php

    Be sure to start at the top and read the 7-8 manual pages it links to in sequence.


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
  •