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
    New Coder
    Join Date
    Feb 2009
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Simple Function Variable Question

    When a variable is passed to a function in the format:
    PHP Code:
    function myfunction(&$myvariable1
    What is the purpose of the ampersand before the variable?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    By reference. It means any changes you perform on the variable within the function will be reflected outside of the function. The default handling of function variables is by copy with the exception of objects, which are always by ref (5.x+).
    The reverse also applies, if you declare a function as a reference:
    PHP Code:
    function &myfunction() 
    The result stored will be updated if the data changes within the function at a later time. This is handy when doing things like last minute dumps from a template system, but are not seen often otherwise.

    Edit:
    btw for further reading: http://php.net/manual/en/language.references.php
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    By reference. It means any changes you perform on the variable within the function will be reflected outside of the function. The default handling of function variables is by copy with the exception of objects, which are always by ref (5.x+).
    The reverse also applies, if you declare a function as a reference:
    PHP Code:
    function &myfunction() 
    The result stored will be updated if the data changes within the function at a later time. This is handy when doing things like last minute dumps from a template system, but are not seen often otherwise.
    Thank you for the quick response!

    This is exactly what I was hoping, and what I figured. Rather difficult to google without knowing the proper terminology.


  •  

    Posting Permissions

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