I have something like this 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 T_ECHO. Changing echo for print solves the issue, but I would like to understand why I cannot use echo.
You can’t use this construct that way. The ternary operator is not an “if” block, but returns a value based on whether the condition is fulfilled or not.
You want to change the structure:
it works with
print()because that is a function with a return value. It is however not what you want, because the firstechowill print out the result ofprintwhich makes no sense.It doesn’t work with
echobecause echo is not a function, but a language construct.