W3docs

echo

The "echo" keyword is a function in PHP that is used to output one or more strings. In this article, we will explore the syntax and usage of the "echo" keyword

The PHP echo Statement

echo is the most common way to send output from a PHP script to the browser (or the console). It is a language construct, not a true function, which is why you will often see it used without parentheses. This page covers its syntax, how it differs from print, the shorthand <?= ?> tag, and the gotchas that trip up beginners.

If you are new to outputting data in PHP, it also helps to read PHP echo and print for a side-by-side comparison and PHP variables to understand the values you will be printing.

Syntax

echo expression1, expression2, ...;
  • Arguments are separated by commas.
  • Because echo is a language construct, parentheses are optional. echo "Hi"; and echo("Hi"); both work — but note that echo("a", "b"); is a syntax error, since parentheses only wrap a single argument.

Basic output

<?php

echo "Hello World!";        // Hello World!
echo "\n";                   // newline
echo "Hello", " ", "World!"; // Hello World!

Passing several arguments separated by commas is marginally faster than building one string with the concatenation operator (.), because PHP does not have to create the joined string first.

Echoing variables

<?php

$name = "John";
$age  = 30;

// Concatenation with the . operator
echo "My name is " . $name . " and I am " . $age . " years old.";

Inside double-quoted strings PHP also interpolates variables directly, which is often cleaner than concatenation:

<?php

$name = "John";
echo "Hello, $name!";        // Hello, John!
echo "Hello, {$name}san!";   // Hello, Johnsan! (braces clarify the variable name)

Single-quoted strings do not interpolate — echo 'Hello, $name'; prints the literal Hello, $name. See PHP strings for the full rules.

Try it Yourself isn't available for this example.

The short echo tag <?= ?>

When you mix PHP into HTML, <?= ?> is shorthand for <?php echo ?>. It is enabled by default in PHP and is the idiomatic way to print a single value inside a template:

<p>Welcome, <?= $username ?>!</p>

<!-- equivalent to: -->
<p>Welcome, <?php echo $username; ?>!</p>

echo vs print

Both produce output, but there are differences:

Featureechoprint
Multiple argumentsYes (comma-separated)No (one only)
Return valueNoneAlways returns 1
Usable in expressionsNoYes (e.g. $ok = print "Hi";)
SpeedMarginally fasterMarginally slower

Because print returns 1, it can appear inside an expression; echo cannot. See The PHP print statement for details.

Common gotchas

  • No value is returned. $x = echo "Hi"; is a fatal error — echo cannot be assigned or used in expressions.
  • Parentheses with commas fail. echo("a", "b"); is invalid; drop the parentheses: echo "a", "b";.
  • Escaping user input. Echoing untrusted data directly into HTML invites XSS. Pass it through htmlspecialchars() first: echo htmlspecialchars($comment);.
  • PHP_EOL vs "\n". Use the PHP_EOL constant when you need the correct line ending for the current operating system (for example, when writing to the console rather than a browser).

Practice

Practice
What is the correct way to use the echo function in PHP?
What is the correct way to use the echo function in PHP?
Was this page helpful?