SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    & (ampersand) before variable

    What does an ampersand before a variable indicate?

    exampe:
    function GetResponseUntilPrompt(&$r) { //code }

    thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,454
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    by reference

    I think it's explained in the docs under variables. The ampersand means that the variable is a "reference" to the actual variable and not a "copy" of it. That is, anything you do to the variable affects the actual variable. - But read the docs for a better explanation.

  3. #3
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I think it's explained in the docs under variables. The ampersand means that the variable is a "reference" to the actual variable and not a "copy" of it. That is, anything you do to the variable affects the actual variable. - But read the docs for a better explanation.
    Thanks. I chose the 'easy' route by asking in the forum. I tried searching google for "php &" but of course all my results were php & mysql related.

  5. #5
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Just in case you're still wondering, basically...

    When you send a variable into a function, it basically creates a copy of that variable. So, any changes to the variable will be made to the copy, not to the original variable. If you don't want the variable copied, and want any changes to the original variable to be reflected in the class/function, you should use a reference to the variable (& before the variable name).

    This means that what you passed into the function is a REFERENCE to the variable, not a copy of the variable. So, whenever updates are made to the original variable, they will be reflected wherever you passed-by-reference.

    Hope that helps. The articles linked to before should be more specific.

  6. #6
    SitePoint Evangelist vhogarth's Avatar
    Join Date
    Nov 2003
    Location
    Taxachussets
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mellymell View Post
    Hi.

    Just in case you're still wondering, basically...

    When you send a variable into a function, it basically creates a copy of that variable. So, any changes to the variable will be made to the copy, not to the original variable. If you don't want the variable copied, and want any changes to the original variable to be reflected in the class/function, you should use a reference to the variable (& before the variable name).

    This means that what you passed into the function is a REFERENCE to the variable, not a copy of the variable. So, whenever updates are made to the original variable, they will be reflected wherever you passed-by-reference.

    Hope that helps. The articles linked to before should be more specific.

    Very much so, thanks.


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
  •