Jump to content

if inside a echo


ecabrera

Recommended Posts

Can there be an if inside an echo

 

 

for example

 

echo "
    <div id='navigation'>
<ul id='nav'>
<!-- main item 1 -->
<li class='item1'>
<a href='".strtolower(str_replace(' ', '-', $cat['title'])).".htm'>".str_replace('and', '&', $cat['id'])."</a>
<!-- dropdown or flyout 1 -->

if($subcat['id'] == $subcat['parent']){
<ul>
<li><a href='#nogo'>{$subcat['title']}</a></li>
</ul>
}else{

}

</li>
</ul>
</div>
    
    ";

Link to comment
Share on other sites

Why are you bothering echoing all that HTML in the first place? Just seems weird. Your IDE won't even give you hinting.  :confused: 

 

Anyway, that won't work because it's going to be parsed as a string and not server-side language. The best way to do conditionals inline with your code is to use ternary and concatenation.  if it's too complex a conditional, just close the echo statement.

 

<?php
$colors = true;

echo ' colors are ' . ($colors == true ? 'enabled' : 'not enabled');

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.