SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Return by reference question

    Hi!

    When I have a function like this:

    PHP Code:
    function &get_something
    {
      
    //
      
    return $something//return a reference

    When I assign that function to a variable, do I still have to do:
    PHP Code:
    $var =& get_something(); 
    or can I simply do:
    PHP Code:
    $var get_something(); 
    ?

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    easier. since you're passing by reference, you don't need to return it in the value or assign it in the code:
    PHP Code:
    function stuff(&$var)
    $var $var ' world'; }
    $mssg 'hello';
    stuff($mssg);
    echo 
    $mssg;
    // echos: hello world 

  3. #3
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either I don't understand what you mean or I didn't express myself clearly..

    I'm talking about return by reference, not pass by reference.

    For example, in my code, the get_something() function could return a class.

    What I want to know is if I need the extra "&" when assigning the return value to the variable.

    PHP Code:
    $var &= get_something(); //is the & rebundant here given the fact that get_something returns by reference? 


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
  •