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
https://forums.phpfreaks.com/topic/265930-if-inside-a-echo/
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
https://forums.phpfreaks.com/topic/265930-if-inside-a-echo/#findComment-1362635
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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