Jump to content

How to format an echo statement to include a condition


Recommended Posts

I've been trying to format an echo statement to include a condition but I always have to split it into two or three echo  statements.


echo '</a>
<td class="info">
<a class="subject" href="', $board['href'], '" name="b', $board['id'], '">', $board['name'], '</a>';


Say I start with the above I'll then make it this


echo '</a>


<td class="info">';

if (this) {echo 'That';}

echo '<a class="subject" href="', $board['href'], '" name="b', $board['id'], '">', $board['name'], '</a>';


How can I include a condition with out extra echo's? I've tried this with and with out brackets and parenthesis and of course it doesn't work.


echo '</a>


<td class="info">'.{if (this) {echo 'That';} }.'

<a class="subject" href="', $board['href'], '" name="b', $board['id'], '">', $board['name'], '</a>';



Link to comment
Share on other sites

If you do all of the processing and parsing prior to writing any output to the screen, you'll find that things become a lot easier:

$extraInfo = '';

if (this) {
    $extraInfo = '<p>And some extra information</p>';

echo <<<Output
<p>This contains some information.</p>

Link to comment
Share on other sites

Thanks for the replys


This is in a loop and it works


$image = FALSE;
if ($board['id'] == 9) {$image = '9999999';}

echo '
<td class="info">'.$image.' '.'
<a class="subject" href="', $board['href'], '" name="b', $board['id'], '">', $board['name'], '</a>';


However when I take out the $image = FALSE; $image prints in all the loop's results. Why does it do that? Shouldn't $image be empty?


Anyway this works and I'll make a switch statement later

if ($board['id'] == 9) {$image = '9999999';} else {$image = FALSE;} and it works fine

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.