PHP: Difference between echo and print
Going over some PHP code I noticed that
Print takes only one parameter
Another difference is that while
echo can take multiple parameters,
echo "Output is one, ", "two", "three"; print("Single argument");
echo does not behave like a function
echo does not behave like a function.
You don't need to use parentheses with either of them, and, most of the time you can use them interchangeably.
$condition = TRUE; ($condition) ? print "true" : print "false"; true
$condition = TRUE; ($condition) ? echo "true" : echo "false"; //PHP Parse error: syntax error, unexpected 'echo' (T_ECHO)
To make it work with
echo we just have to update the statement to:
$condition = TRUE; echo $condition ? "true" : "true"; //PHP Parse error: syntax error, unexpected 'echo' (T_ECHO)
This might seem a little bit contrieved, but it's only an example. A situation where I like to take advantage of the fact that
//Usually quick debug fix myMethod() AND print('myMethod: OK') OR print('myMethod: KO'.generateError());
echo is faster
echo but not worth taking into account. Really.