zimmo Posted September 9, 2010 Share Posted September 9, 2010 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 More sharing options...
schilly Posted September 9, 2010 Share Posted September 9, 2010 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/212970-if-else-within-an-echo/#findComment-1109237 Share on other sites More sharing options...
zimmo Posted September 9, 2010 Author Share Posted September 9, 2010 Thanks that has helped me as I will alter to write the way you have put. THanks Link to comment https://forums.phpfreaks.com/topic/212970-if-else-within-an-echo/#findComment-1109249 Share on other sites More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 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 Link to comment https://forums.phpfreaks.com/topic/212970-if-else-within-an-echo/#findComment-1109250 Share on other sites More sharing options...
schilly Posted September 9, 2010 Share Posted September 9, 2010 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. =) Link to comment https://forums.phpfreaks.com/topic/212970-if-else-within-an-echo/#findComment-1109254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.