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

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
User avatar
apexonelove
Forum Newbie
Posts: 7
Joined: Tue Jul 17, 2007 2:52 pm
Location: USA

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

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
apexonelove
Forum Newbie
Posts: 7
Joined: Tue Jul 17, 2007 2:52 pm
Location: USA

Post by apexonelove »

You, sir, are a pimp. Thanks much.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Was that a compliment, scottayy?? :lol:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

best compliment i've gotten yet!

PHPimp.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply