I use to write one line if statements combined with echo like this:
<?php echo ( true ) ? 'true' : 'false'; ?>
Today I did alter en existing multi line if statement and the echo
ended up inside the statement, which gave me a parse error:
<?php ( true ) ? echo 'true' : echo 'false'; ?>
Using print
instead of echo
makes it work, though. I figure that it works because print
is a function. Update: print
is not a function it just behaves like one, which means it has a return value.
<?php ( true ) ? print 'true' : print 'false'; ?>
What I don't understand is the reason why echo doesn't work. As i understand is the above syntax just a shorthand for a common if statement, so this shouldn't be working either:
if (true) echo 'true'; else echo 'false';
But it does. Someone who knows?
echo
then write your ternary expression to the right of it. – Bocock