Use echo inside php one line if statement [duplicate]
Asked Answered
C

3

9

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?

Coles answered 3/12, 2013 at 8:45 Comment(3)
This might be helpfulThrilling
answer inside the question only.Jemmy
start with echo then write your ternary expression to the right of it.Bocock
R
2

As you only mentioned in your question that print is a function but print is not a function but it has some return value thats why it can be used in expressions also but on the other hand echo doesnot have any return value.

also keep in mind that Ternary operator returns a value.

"In C-alike languages there is a distinction between statements and expressions. Syntactically, echo is a (simple) statement, like break or return and print is an (unary) operator, like "!" or "~". Therefore, like any other statement, echo cannot be part of an expression"

PHP treat echo as a statement.whatever you will write that will be displayed as it is.

you can read the detailed explanation here Reference: Comparing PHP's print and echo

Rejoice answered 3/12, 2013 at 9:4 Comment(1)
@oGeez correct print is not a function but it returns value irrespective of echo,i updated my answerRejoice
C
3

As you can read from the PHP documentation:

http://php.net/manual/en/language.operators.comparison.php

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

The ternary operator expects expressions, which is just a fancy way of saying a value. Echo does not return anything and so has no value meaning it's not an expression.

Print on the other hand, returns something since it's a function, making it a valid expression.

Colenecoleopteran answered 3/12, 2013 at 9:8 Comment(0)
R
2

As you only mentioned in your question that print is a function but print is not a function but it has some return value thats why it can be used in expressions also but on the other hand echo doesnot have any return value.

also keep in mind that Ternary operator returns a value.

"In C-alike languages there is a distinction between statements and expressions. Syntactically, echo is a (simple) statement, like break or return and print is an (unary) operator, like "!" or "~". Therefore, like any other statement, echo cannot be part of an expression"

PHP treat echo as a statement.whatever you will write that will be displayed as it is.

you can read the detailed explanation here Reference: Comparing PHP's print and echo

Rejoice answered 3/12, 2013 at 9:4 Comment(1)
@oGeez correct print is not a function but it returns value irrespective of echo,i updated my answerRejoice
R
2

in PHP, print and echo is not function but language construct and basically print and echo does not need using parentheses.

the difference is that print is laguage construct and can behaves like as function but echo can't!

PHP:echo - Manual

Romany answered 3/12, 2013 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.