Unexpected T_ECHO tag in string if
I have something similar in one of my views
<li <?php $isCurrent ? echo 'class="current"' : ''?> >
<a href="SOME_LINK" class="SOME_CLASS">SOME_TEXT</a>
</li>
This causes a syntax error, unexpected by T_ECHO. Changing echofor printsolves the problem, but I would like to understand why I can not use echo.
+3
6 answers
You cannot use this construct in this way. the ternary operator is not an "if" block, but returns a value based on whether the condition is met or not.
You want to change the structure:
<?php echo ($isCurrent ? 'class="current"' : '') ?>
print(), . , , echo print, .
echo, , .
+9