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
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>';

?>

 

Link to comment
Share on other sites

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
Share on other sites

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

Guest
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.