Web Analytics Made Easy -
StatCounter Return an array from a function - CodingForum

Announcement

Collapse
No announcement yet.

Return an array from a function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    How are you calling and storing the results of the function?
    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 ;)

    Comment


    • #3
      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

      Comment


      • #4
        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.
        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 ;)

        Comment


        • #5
          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'];

          Comment


          • #6
            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); 
            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 ;)

            Comment


            • #7
              Ahh i see , thank you. Just curious whats the & do infront of $aInto? Ive never seen that before.

              Comment


              • #8
                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.
                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 ;)

                Comment


                • #9
                  Ahh okay, thanks .

                  Comment

                  Working...
                  X
                  😀
                  🥰
                  🤢
                  😎
                  😡
                  👍
                  👎