ecabrera Posted July 19, 2012 Share Posted July 19, 2012 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> "; Quote Link to comment https://forums.phpfreaks.com/topic/265930-if-inside-a-echo/ Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 No. You could use the ternary operator if you don't mind concatenating, though. Quote Link to comment https://forums.phpfreaks.com/topic/265930-if-inside-a-echo/#findComment-1362634 Share on other sites More sharing options...
Mahngiel Posted July 19, 2012 Share Posted July 19, 2012 Why are you bothering echoing all that HTML in the first place? Just seems weird. Your IDE won't even give you hinting. 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'); Quote Link to comment https://forums.phpfreaks.com/topic/265930-if-inside-a-echo/#findComment-1362635 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.