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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Return an array from a function

    I have this code

    PHP Code:
    function getUserDetails($ID)
    {
        
    $Sql "SELECT * FROM members WHERE user_id = '$ID'";
        
    $Res mysql_query($Sql);
        
        while (
    $row mysql_fetch_array($Res))
        {
            foreach (
    $row as $ide => $val)
            {
                
    $userDetails[$ide] = $val;    
            }
        }

        return 
    $userDetails;

    on a functions.php page which is an include on my inc.header page (so every page has the functions). If i call that function at the start of a page and try echo $userDetails['username']; it doesnt return anything but if in the function i put that it outputs the username.

    What am i doing wrong? Or is this undoable.

    Thanks

  • #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
    How are you calling and storing the results of the function?

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    I called it by doing getUserDetails($_SESSION['userID']);

    Howeer i put it into a class and i got it working that way, all i need to do to get info is $user->userDetails['whatever i want'];

    I didnt think of using a class when i made the function :P

  • #4
    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
    The only difference between a class and a function is the addition of a context.
    When you call a method, same as a function, you need to assign the results to something. In the case of a class, you have assigned it to a member which can be accessed as a part of the object. In a function, you need to actually assign the results: $userDetails = getUserDetails($_SESSION['userID']);. $userDetails in the scope of the call is not the same as the $userDetails in the scope of the function. They are completely different memory sets, so there is no relation to them whatsoever.

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay so do you mean i should have called the function like

    $user = getUserDetails($_SESSION['userID']);

    Then got the values by doing this?

    $user = $userDetails['username'];

  • #6
    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
    No, $user would contain the results of getUserDetails. You would access it under $user['username'].
    Alternatively, methods can be written to accept a reference to write directly into:
    PHP Code:
    function loaduser($iUserID, &$aInto)
    {
        
    $sQry 'SELECT * FROM user WHERE userid = ' $iUserID;
        if (
    $q mysql_query($sQry))
        {
            
    $aInto mysql_fetch_assoc($q);
        }
    }

    $user = array();
    loaduser(1$user); 

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Ahh i see , thank you. Just curious whats the & do infront of $aInto? Ive never seen that before.

  • #8
    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
    It creates a reference to the calling variable. By default, methods and functions with the exceptions of objects will perform operations on a copy of the variable, not the original. Using a reference forces all changes to occur on the original.
    Functions can also return by reference so any time a variable is assigned to a function result and later the same function is called again but produces different results would be reflected in the previous variable assignment as well. There are very very few uses for return by reference techniques.

  • Users who have thanked Fou-Lu for this post:

    tomharto (08-30-2011)

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Ahh okay, thanks .


  •  

    Posting Permissions

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