Jump to content

if else within an echo


zimmo

Recommended Posts

What is the correct way to write an if/else statement within an echo? I need to alter this so that I can query to see what data is found and if not correct not to echo the rest of the statement.

 

echo '<td class="productbox">
<h1>' . $product_title . '</h1>
</td>'; 

 

So from the above code which is echoed within  the single quotes, what is the correct way to include an if else check on a value from the database.

 

I know how its done, but just want to save time and write it the correct way within this.

Link to comment
https://forums.phpfreaks.com/topic/212970-if-else-within-an-echo/
Share on other sites

depends what you want to output. you won't write a if/else inside an echo.

 

shows us the full info and what you want filtered based on the db value.

 

i would normally write your statement like:

<?php

echo '<td class="productbox">';
echo '<h1>' . $product_title . '</h1>';
if(condition)
//echo other data if condition is met else dont output it
echo '</td>';

?>

 

Hi all,

 

You can *sort of* do an if/else inside an echo; simply by using a nested ternary:-

echo "this is a ".((isset($weather) && $weather == "nice") ? 'Sunny':'cloudy')." day";

 

Put simply means:-

 

((conditions_to_evaluate) ? 'condition evaluates true' :'condition evaluates false')

 

Just to give you the idea of logic there :-p

 

Cheers,

Rw

Hi all,

 

You can *sort of* do an if/else inside an echo; simply by using a nested ternary:-

echo "this is a ".((isset($weather) && $weather == "nice") ? 'Sunny':'cloudy')." day";

 

Put simply means:-

 

((conditions_to_evaluate) ? 'condition evaluates true' :'condition evaluates false')

 

Just to give you the idea of logic there :-p

 

Cheers,

Rw

 

ah right. never thought of doing that. =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.