return variable from function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

return variable from function

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: return variable from function

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 3:44 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: return variable from function

Post 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...
Post Reply