Page 1 of 1

return variable from function

Posted: Sun Dec 13, 2009 11:32 am
by gth759k
I have a function that echos off the messages a person has in the database like this:

Code: Select all

 
// messages.php
function messages($user) {
    $sql = "SELECT* FROM messages WHERE user='$user'";
    $result = mysql_query($sql) or die(mysql_error());
    
    $i = 0;
    while  ($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
          // a bunch of stuff happens
          $i = $i + 1;
    }
    $totalmessages = $i;
}
 
in another file I have all the html and I include messages.php and call the function messages like this:

Code: Select all

 
    // inbox.php
    // a bunch of stuff
    include('messages.php');
    echo '<div id="messages">';
        messages($_COOKIE['username']);
    echo '</div>';
 
what I want to do is take actions depending on the number of messages from the inbox like this

Code: Select all

 
    // inbox.php
    // a bunch of stuff
    include('messages.php');
    echo '<div id="messages">';
        messages($_COOKIE['username']);
    echo '</div>';
    if ($totalmessages > 10) {
        // do something
    }
 
but I can't seem to access the $totalmessages variable from here. I know I can do it without using a function, but I'm calling the function from more that one place, so I wouldn't have to have the same script in multiple places in case I change my database. Any help would be appreciated. Thanks.

Re: return variable from function

Posted: Sun Dec 13, 2009 12:22 pm
by McInfo
Either return the total from the function:

Code: Select all

function messages($user) {
    echo '<div></div>';
    $totalmessages = 3;
    return $totalmessages;
}
$total = messages('user'); // Displays "<div></div>"
echo $total; // Displays "3"
Or use a by-reference variable:

Code: Select all

function messages($user, &$totalmessages) {
    echo '<div></div>';
    $totalmessages = 3;
}
$total = 0;
messages('user', $total); // Displays "<div></div>"
echo $total; // Displays "3"
You can make the $totalmessages parameter optional by giving it a default value of NULL.

Code: Select all

function messages($user, &$totalmessages = null)
The function would be more useful if, rather than echoing the HTML from within itself, it returns a string.

Code: Select all

function messages($user, &$totalmessages = null) {
    $html = '<div></div>';
    $totalmessages = 3;
    return $html;
}
$total = 0;
echo messages('user', $total); // Displays "<div></div>"
echo $total; // Displays "3"
I used different names for $totalmessages and $total to show that they are two different variables. However, they could be given the same name.

Edit: This post was recovered from search engine cache.

Re: return variable from function

Posted: Sun Dec 13, 2009 3:41 pm
by requinix
I sure hope your script validated the cookie. Made sure the username matched the password or hash or whatever else you're storing in it.

People can put whatever they want into a cookie - like somebody else's username...