Beginner Fundamentals

Echo and Print

PHP gives you two simple ways to send output to the page: echo and print. Both display text, with small differences.

Using echo

echo outputs one or more values. It is the most common choice.

<?php
echo "Hello, world!";
echo "One", " ", "Two"; // echo can take multiple values

Using print

print outputs a single value and always returns 1, so it can be used in expressions.

<?php
print "Hello from print";
$result = print "Returns a value";

Difference between them

  • echo is slightly faster and accepts multiple arguments.
  • print returns a value and takes only one argument.

In practice, echo is used most of the time.

Concatenation with the dot

Join strings together using the dot operator ..

<?php
$first = "John";
$last = "Doe";
echo $first . " " . $last; // John Doe

You can also embed variables directly inside double quotes.

<?php
$name = "Sara";
echo "Hello, $name!"; // Hello, Sara!