echoing variables

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
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

echoing variables

Post by bloodl »

Hiya!

Can anyone please help me?

I wanted to have a default value of an echo is the variable has no value. Its in some html code:

Code: Select all

<td><?php echo $username; ?> or if null, then echo this "default echo here"</td>
What would I write here?

Cheers,
Doug
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

<?php
if (!empty($username))
{
    echo $username;
} else
{
    echo 'default value here';
}
?>
Or, in a simple one liner

Code: Select all

<?php echo !empty($username) ? $username : 'default value here'; ?>
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.
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

WOOHOO!

Post by bloodl »

thanks, very useful indeed -

Thanks for taking the time

Cheers,
Doug
Post Reply