Page 1 of 1

echoing variables

Posted: Thu Nov 22, 2007 11:10 pm
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

Posted: Thu Nov 22, 2007 11:29 pm
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'; ?>

WOOHOO!

Posted: Thu Nov 22, 2007 11:57 pm
by bloodl
thanks, very useful indeed -

Thanks for taking the time

Cheers,
Doug