Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts

    & before a variable

    At times I see variables like so:

    &$array with the "&" before the variable name. I couldn't find an explanation of it anywhere, could anyone explain what it stands for?

    Duniyadnd

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The "&" before the variable name is used to assign a reference to that variable. By default, PHP assigns all variables by value, which is quite dumb when you work a lot with objects and very inconsistent in regard to other OOP languages. Gladly, this matter is subject to change in PHP5.

    But back to references: They are quite good explained at http://www.php.net/manual/en/languag...es.whatdo.php, so I only want to point out that should be aware that in newer PHP versions you only define references as arguments during the function definition and not at runtime by invoking the function, i.e.

    PHP Code:
    function foo(&$bar) {
    do 
    sth.
    }

    $test =& new Bar();
    foo($test); 
    is correct syntax, whereas

    PHP Code:
    function foo($bar) {
    // do sth.
    }

    $test =& new Bar();
    foo(&$test); 
    would be not good in newer PHP versions and prompt an error notice. I know that's kinda confusing, so if you feel like... don't hesitate to ask.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, makes sense. I just didn't know it meant it was a reference..

    Helps that you explained which way to use it in this language as well.

    Thanks
    Duniyadnd


  •  

    Posting Permissions

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