echo and print
echo and print are the two basic ways to output data with PHP.
Syntax
echo "text";
print "text";PHP offers two language constructs to output text: echo and print.
Differences
echocan take several arguments separated by commas and has no return value. It is marginally faster.printtakes a single argument and always returns1, so it can be used in expressions.
Neither requires parentheses.
Example
Loading editor…
Press Run to execute the code.
When to use it
- Sending HTML or text to the browser from PHP.
- Printing values while debugging.
- Concatenating dynamic content into a page.
More examples
echo takes multiple arguments
echo can output several strings separated by commas.
<?php
echo "a", "b", "c";print returns a value
print always returns 1 so it works inside expressions; echo does not return anything.
<?php
$r = print "Hello";
echo "\n" . $r; // 1Concatenate with a dot
The dot (.) joins strings together.
<?php
$name = "Ada";
echo "Hi, " . $name . "!";
Discussion