PHP Echo & Print
echo and print are used to output data to the screen. They are very similar, but echo can take multiple, comma-separated parameters, while print can only take a single argument and always returns 1.
Both echo and print are language constructs, not real functions, so parentheses are optional. Because echo is marginally faster and more flexible, it is the more commonly used of the two.
echo "text", $var;
print "text";Using echo
echo can output strings, numbers, and variables, and can accept several comma-separated values in one call, printing them next to each other with no separator.
Using print
print behaves like echo but only accepts one argument and returns the integer 1, which means it can technically be used inside an expression.
<?php
echo "Hello", " ", "World!";
?>Hello World!echo joins multiple comma-separated arguments with no extra spacing added automatically.
<?php
$age = 25;
print "Age: " . $age;
?>Age: 25print takes a single string, built here using the concatenation operator.
Key points
- echo and print both output data to the page.
- echo can accept multiple comma-separated arguments; print accepts only one.
- Parentheses are optional for both echo and print.
- print always returns the value 1.
