Page 1 of 1

php - string or function? (multiple on single page)

Posted: Mon Oct 08, 2007 3:40 pm
by apexonelove
I'm somewhat of a novice when it comes to strings and functions...and running into a bit of a wall.

The Situation:
I've got a simple query that I'm trying to echo over and over again within a single page (username if chosen or first and last name as else).

I've tried working with strings and functions to no avail when it comes to outputting them more than once. To save on the coding and size of the file, I'd love to do this function once and then just echo the function where is needed.

Here's my if/then statement that I'd like to make into a repeatable function:

Code: Select all

<?php 
if($row_rsUserDetails['use_username'] == 1) 
{
echo $row_rsUserDetails['Username'];
}
else
{
echo $row_rsUserDetails['f_name'];
echo " ";
echo $row_rsUserDetails['l_name'];
}
?>
Any help would be amazing!

Posted: Mon Oct 08, 2007 3:45 pm
by s.dot

Code: Select all

function show($row_rsUserDetails)
{
    if($row_rsUserDetails['use_username'] == 1)
   {
        return $row_rsUserDetails['Username'];
    }
    else
    {
        $ret = $row_rsUserDetails['f_name'];
        $ret .= " ";
        $ret .=  $row_rsUserDetails['l_name'];
        return $ret;
    }
} 

echo show($row_rsUserDetails);
:) I've just stuck your if/else in a function, and used a return value instead of echo. You're passing the entire row as a parameter. Then when you echo the function, it'll echo what you desire.

Posted: Mon Oct 08, 2007 3:55 pm
by apexonelove
You, sir, are a pimp. Thanks much.

Posted: Mon Oct 08, 2007 6:37 pm
by califdon
Was that a compliment, scottayy?? :lol:

Posted: Mon Oct 08, 2007 8:11 pm
by s.dot
best compliment i've gotten yet!

PHPimp.