floridaflatlander Posted August 23, 2012 Share Posted August 23, 2012 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> <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> <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> <td class="info">'.{if (this) {echo 'That';} }.' <a class="subject" href="', $board['href'], '" name="b', $board['id'], '">', $board['name'], '</a>'; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/ Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 You could use the ternary operator, or set the values in variables before trying to echo them. http://us3.php.net/ternary Scroll down. Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/#findComment-1371869 Share on other sites More sharing options...
Christian F. Posted August 23, 2012 Share Posted August 23, 2012 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> $extraInfo Output; Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/#findComment-1371872 Share on other sites More sharing options...
floridaflatlander Posted August 23, 2012 Author Share Posted August 23, 2012 Thanks for the replys This is in a loop and it works $image = FALSE; if ($board['id'] == 9) {$image = '9999999';} echo ' </a> </td> <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 Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/#findComment-1371892 Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 "Shouldn't $image be empty?" Not if it's in a loop. Once you set it, it stays that value. Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/#findComment-1371893 Share on other sites More sharing options...
floridaflatlander Posted August 23, 2012 Author Share Posted August 23, 2012 That works Thanks Quote Link to comment https://forums.phpfreaks.com/topic/267495-how-to-format-an-echo-statement-to-include-a-condition/#findComment-1371898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.