SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does the =& mean in PHP

    Hey guys,

    I know this probably a pretty silly question. I have done some pretty basic PHP development in the past but I'm trying to get more deeply involved. Anyways as I was working through some code samples I came across "=&" but I can't seem to find any reference to it in the documentation.

    What is it supposed to do?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In what context?

  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thenergle View Post
    Hey guys,

    I know this probably a pretty silly question. I have done some pretty basic PHP development in the past but I'm trying to get more deeply involved. Anyways as I was working through some code samples I came across "=&" but I can't seem to find any reference to it in the documentation.

    What is it supposed to do?
    It has to do with 'passing by reference'.
    Look it up here:
    http://us3.php.net/manual/en/languag...ences.pass.php

  4. #4
    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)
    If it's in an object context (i.e. $something =& new Something(); ) then it's outdated PHP4 code.

    As for other variables, here's a nice example for you:
    PHP Code:
    <?php
    $A1 
    11;
    $A2 $A1;
    $A2++;
    echo 
    'A1 : ' $A1 '<br />A2 : ' $A2;
    echo 
    '<hr />';
    $B1 11;
    $B2 =& $B1;
    $B2++;
    echo 
    'B1 : ' $B1 '<br />B2 : ' $B2;
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example of something I was looking through. This code is from one of the Joomla content component models:

    PHP Code:
    function getData($state 1)
        {
            
    // Load the Category data
            
    if ($this->_loadCategory() && $this->_loadData($state))
            {
                
    // Initialize some variables
                
    $user    =& JFactory::getUser();

                
    // Make sure the category is published
                
    if (!$this->_category->published)
                {
                    
    JError::raiseError(404JText::_("Resource Not Found"));
                    return 
    false;
                }

                
    // check whether category access level allows access
                
    if ($this->_category->access $user->get('aid'0))
                {
                    
    JError::raiseError(403JText::_("ALERTNOTAUTH"));
                    return 
    false;
                }
            }
            return 
    $this->_data[$state];
        } 
    The line "$user =& JFactory::getUser();"

  6. #6
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    If it's in an object context (i.e. $something =& new Something(); ) then it's outdated PHP4 code.

    As for other variables, here's a nice example for you:

    So it is in the object context and related to PHP4. But if I am understanding the sample you gave me, the $B2 =& $B1 is essentially resetting the $B1 variable to take on the characteristics of $B2.

  7. #7
    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)
    Not quite.

    When you declare a variable, it is allocated a certain memory slot.

    So assigning variable1 to the value of variable2, the memory slot from variable2 will be copied to a fresh slot and variable1 will be assigned to that slot.

    If you assign it using the pointer, rather than giving a fresh memory slot to a new variable, variable1 will be assigned to the same slot as variable2.

    In other words, by saying $variable1 =& $variable2, you're saying that variable1 and variable2 are referring to exactly the same thing, and by changing one, the other will be effected too. You're you're essentially saying that $variable1 IS ANOTHER NAME FOR $variable2.

    As for the issue with PHP4 - when you assign a variable to an object, the expected behaviour in OOP languages is for it to be a reference. In PHP4, this was done incorrectly so it was simply passed by value.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, okay that helps explain a lot. Thanks so much for your quick responses. I appreciate it.

  9. #9
    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)
    That's what we're here for

    If you have any questions related to PHP, no matter how small, don't hold yourself back from asking them here (as long as you've tried Google before-hand).
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •