//In PHP,
$a ? $b : echo $c //does not work but
$a ? $b : print $c //works
Is there a reason for this?
BTW,is not
a keyword in PHP?
//In PHP,
$a ? $b : echo $c //does not work but
$a ? $b : print $c //works
Is there a reason for this?
BTW,is not
a keyword in PHP?
return value. if you wrote
$x = $a?$b:echo $c;
what value would $x get on $a being false?
print always returns 1. echo doesn't return anything.
echo
does not have a return value, print
does!
void echo ( string $arg1 [, string $... ] )
int print ( string $arg )
from php.net
This matters in this case since the ternary operator expects expressions
(expr1) ? (expr2) : (expr3)
echo "something"
cannot be classified as an expression.
not
is not a PHP keyword.
echo
is not a function, it's language construct. It does not return anything. Another void
functions always "return" NULL
. It's why the compiler throws error unexpected T_ECHO
.
printf
, however, is a function and always returns 1
.
For example this will work:
function echo2($e)
{
echo($e);
}
$a ? $b : echo2('foo');
Is it because echo is a language construct and print is a function?
In c-alike languages there is a difference between statements (like "if", "while" etc) and operators (like +, -, casts etc). Unlike operators, statements cannot be used in expressions, for example, "10 + (if(1) 3 else 4)" is invalid. In php, "echo" is a statement, and "print" is an operator (don't ask). That's why $a = 10 + print 20
is possible, but not $a = 10 + echo 20