Newbie: PHP Syntax

Discussion in 'PHP' started by zaphod2003, Jul 6, 2007.

  1. #1
    Hi there

    Could someone explain what &$ does as in a function like

    function checkArray(&$array){
    }
     
    zaphod2003, Jul 6, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Jul 6, 2007 IP
  3. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Normally, variables are passed "by value" which means a copy of the variable is pushed onto the stack, before the function call and pulled off the stack by the function itself. Any changes to the array are not reflected back in the calling routine.

    Adding "&" on the front passes by reference. This means the address of the array is pushed onto the stack. The function can then directly manipulate the original array and results would be reflected back in the calling routine.
     
    ecentricNick, Jul 6, 2007 IP
  4. zaphod2003

    zaphod2003 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Excellent, thanks a lot
     
    zaphod2003, Jul 6, 2007 IP