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. Quote Link to comment 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>'; ?> Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. =) Quote Link to comment 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.