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 page. It is a language construct, not a function, so it has no return value and you do not need parentheses to call it. You will use echo constantly: to print text, render the values of variables, and write HTML.

This page covers the syntax, the single- vs. double-quote behavior that trips up beginners, the short <?= ?> tag, and how echo differs from print.

Syntax

echo expression;
echo expression1, expression2, ...;

Because echo is a language construct, you call it without parentheses:

echo "Hello";        // correct
echo("Hello");       // also works for a SINGLE argument
echo("a", "b");      // PARSE ERROR — parentheses can't wrap multiple args
echo "a", "b";       // correct way to pass multiple arguments

Pass multiple values by separating them with commas — this is slightly faster than concatenating with . because no temporary string is built. The values are printed back-to-back with no separator added.

Examples

Let's look at some practical examples of how the echo keyword can be used:

Examples of PHP echo

<?php

// Example 1: Comma-separated arguments (native syntax)
echo "Hello World!", PHP_EOL;

// Output: Hello World!

// Example 2: String concatenation
$myName = "John";
$myAge = 30;
echo "My name is " . $myName . " and I am " . $myAge . " years old.";

// Output: My name is John and I am 30 years old.

// Example 3: Variables without quotes (comma-separated)
$greeting = "Hello";
$name = "World";
echo $greeting, " ", $name, "!";

// Output: Hello World!

Single Quotes vs. Double Quotes

The quote style you choose changes how variables are handled, so this is worth getting right early. In double-quoted strings, PHP parses variables and escape sequences. In single-quoted strings, it does not — the text is printed literally.

<?php

$name = "World";

echo "Hello $name\n";   // Hello World  (variable is interpolated)
echo 'Hello $name\n';   // Hello $name\n  (printed verbatim, no newline)

Use double quotes when you want variable interpolation; use single quotes (slightly faster) when you have plain, literal text. See PHP Strings for the full rules, including curly-brace syntax like "{$user['name']}".

Echoing HTML

A very common job for echo is printing HTML. You can embed variables directly inside the markup:

<?php

$title = "Welcome";
echo "<h1>$title</h1>";
// Output: <h1>Welcome</h1>

For large blocks of HTML it is usually cleaner to drop out of PHP entirely (?> ... <?php) than to echo every line.

The Short Echo Tag

The shorthand <?= ... ?> is exactly equivalent to <?php echo ... ?>. It is enabled by default in all supported PHP versions and is ideal for templates:

<p>Hello, <?= $name ?>!</p>

This is the recommended way to print a single value inside otherwise-HTML files.

echo vs. print

echo and print look similar, but there are differences:

echoprint
Typelanguage constructlanguage construct
Return valuenone (void)always 1
Multiple argumentsyes (echo $a, $b;)no
Use in an expressionnoyes (because it returns a value)

Because print returns 1, it can appear inside an expression: $ok = print "hi";. echo cannot. In practice echo is preferred for general output. See the dedicated echo vs print comparison and the print reference for details.

Common Gotchas

  • No parentheses with multiple arguments. echo("a", "b"); is a parse error; use echo "a", "b";.
  • echo returns nothing. You cannot write $x = echo "hi";. Use print (or sprintf()) if you need a value.
  • Single quotes don't interpolate. echo 'Total: $price'; prints the literal $price, not the variable's value.

Practice

Practice
What does the PHP 'echo' statement do?
What does the PHP 'echo' statement do?
Was this page helpful?