echo and print

echo and print are the two basic ways to output data with PHP.

Syntaxecho "text"; print "text";

PHP offers two language constructs to output text: echo and print.

Differences

  • echo can take several arguments separated by commas and has no return value. It is marginally faster.
  • print takes a single argument and always returns 1, so it can be used in expressions.

Neither requires parentheses.

Example

Try it yourself
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.

Example · php
<?php
echo "a", "b", "c";

print returns a value

print always returns 1 so it works inside expressions; echo does not return anything.

Example · php
<?php
$r = print "Hello";
echo "\n" . $r; // 1

Concatenate with a dot

The dot (.) joins strings together.

Example · php
<?php
$name = "Ada";
echo "Hi, " . $name . "!";

Discussion

  • Be the first to comment on this lesson.