ZetCode

PHP echo Keyword

last modified November 25, 2026

The PHP echo keyword writes strings and other values to the output. It is the most direct way to produce a page, a log line, or a command-line message.

This article covers the syntax, the comma-separated form, the limits of parentheses, the difference from print, type conversion, arrays and objects, HTML escaping, and the short echo tag used in templates.

Basic definitions

echo writes one or more values to the output. It is a language construct, like if or return, not a function, so it does not need parentheses and it does not return a value.

The forms are echo $value;, echo $first, $second;, and echo($value);. Parentheses are allowed around a single value; they are a parse error around several. The comma form sends each value in order and avoids building a concatenated string first.

Every value is converted to a string using PHP's normal rules: true becomes 1, false and null become an empty string, numbers use their usual textual form, arrays trigger a warning, and objects need __toString(). In HTML, the result must be escaped with htmlspecialchars.

The print keyword is the close alternative; it returns 1 and accepts one argument. This article concentrates on echo and points out the differences where they matter.

A first echo

The smallest useful script writes one string and ends the line.

echo_basic.php
<?php

declare(strict_types=1);

echo "Hello there!\n";

The string ends with \n, which is a newline in the command line. In a browser, a newline in the HTML source does not create a visible line break; use <br>, a paragraph, or CSS. The semicolon ends the statement, and no parentheses are needed for one value.

$ php echo_basic.php
Hello there!

Several arguments

A single echo can write several values. Commas separate the arguments, and each argument is converted and written in order.

echo_arguments.php
<?php

declare(strict_types=1);

$name = "Ada";
$role = "engineer";

echo "Name: ", $name, "\n";
echo "Role: ", $role, "\n";

Each comma-separated expression is evaluated and written in order. The spaces and the newline are ordinary parts of the strings, not separators added by echo. This form is convenient for templates and logs because no temporary concatenated string is created.

$ php echo_arguments.php
Name: Ada
Role: engineer

Parentheses around one value

A single value may be wrapped in parentheses. The output is identical; the parentheses are grouping, not a function call.

echo_paren.php
<?php

declare(strict_types=1);

echo("one argument\n");

The parentheses are optional and have no effect on the output. They can be useful when the argument is a long expression and the grouping makes the statement easier to read.

$ php echo_paren.php
one argument

Parentheses around several values

The parenthesized form does not accept a list. With more than one value, the comma must follow echo directly.

echo_paren_many.php
<?php

declare(strict_types=1);

echo("first", "second");

The parser stops at the comma inside the parentheses. Use echo "first", "second"; without parentheses, or concatenate the values into one expression. The no-parentheses comma form is the only way to pass several arguments.

$ php echo_paren_many.php
Parse error: syntax error, unexpected token "," in /app/echo_paren_many.php on line 5

echo is not a function

echo produces output but no value, so it cannot be used where an expression is required. Assignment is the simplest example.

echo_not_function.php
<?php

declare(strict_types=1);

$result = echo "hello";

echo is not an expression, so it cannot appear on the right side of an assignment, inside a function call, or in a return statement. The parser stops at the keyword. When a value is required, use print, which returns 1, or call a function that returns a string.

$ php echo_not_function.php
Parse error: syntax error, unexpected token "echo" in /app/echo_not_function.php on line 5

Values of different types

PHP converts each argument to a string before writing it. The result depends on the type, and the differences are easiest to see with brackets around the value.

echo_types.php
<?php

declare(strict_types=1);

$flag = true;
$price = 19.99;

echo "true: [", $flag, "]\n";
echo "false: [", false, "]\n";
echo "null: [", null, "]\n";
echo "float: ", $price, "\n";
echo "int: ", 42, "\n";

The brackets make the empty results visible. true prints as 1, while false and null print nothing at all. A float is printed with PHP's current precision settings, so 19.99 stays 19.99 here; calculations that produce repeating decimals may show a rounded form. Use var_dump or var_export when the type matters.

$ php echo_types.php
true: [1]
false: []
null: []
float: 19.99
int: 42

Arrays cannot be echoed

An array has no string form. PHP reports a warning and writes a placeholder instead of the elements.

echo_array.php
<?php

declare(strict_types=1);

$colors = ["red", "green"];

echo $colors, "\n";

The warning is Warning: Array to string conversion, and the output is the word Array, which is almost never what the page should show. Use implode(", ", $colors) for a list, print_r or var_export for debugging, and json_encode when the data crosses a boundary.

$ php echo_array.php
Warning: Array to string conversion in /app/echo_array.php on line 7
Array

Objects and __toString

An object can be echoed when it defines __toString(). PHP calls that method and writes the returned string.

echo_object.php
<?php

declare(strict_types=1);

class User
{
    public function __construct(private string $name)
    {
    }

    public function __toString(): string
    {
        return $this->name;
    }
}

$user = new User("Ada");
echo "User: ", $user, "\n";

The method must return a string; returning anything else raises a TypeError. An object without __toString() cannot be echoed at all: PHP throws an Error saying that the object could not be converted to string. The magic methods article covers __toString in context.

$ php echo_object.php
User: Ada

Escaping HTML output

When the output is HTML, every dynamic value must be escaped. Otherwise a value that contains markup changes the structure of the page, and a value that contains a script becomes an injection.

echo_html.php
<?php

declare(strict_types=1);

$name = "Ada <script>alert(1)</script>";
$safe = htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");

echo "<p>Hello, ", $safe, "</p>\n";

The name contains HTML and a script tag. htmlspecialchars turns the angle brackets and quotes into entities, so the browser displays the text instead of running it. ENT_QUOTES covers both quote styles, and ENT_SUBSTITUTE replaces invalid byte sequences. Escape at the point of output, not when the data is stored.

$ php echo_html.php
<p>Hello, Ada &lt;script&gt;alert(1)&lt;/script&gt;</p>

The short echo tag in templates

A PHP file may mix HTML and PHP. The <?= tag is a shorthand for <?php echo and is the usual way to print a value inside a template.

echo_template.php
<?php

declare(strict_types=1);

$title = "Dashboard";
$items = ["Invoices", "Reports"];
?>
<h1><?= htmlspecialchars($title, ENT_QUOTES, "UTF-8") ?></h1>
<ul>
<?php foreach ($items as $item): ?>
    <li><?= htmlspecialchars($item, ENT_QUOTES, "UTF-8") ?></li>
<?php endforeach; ?>
</ul>

The file switches between HTML and PHP. <?= has been available regardless of the short_open_tag setting since PHP 5.4. Every dynamic value is escaped, and the alternative foreach syntax keeps the HTML readable. A template engine is not required for a page this small.

$ php echo_template.php
<h1>Dashboard</h1>
<ul>
    <li>Invoices</li>
    <li>Reports</li>
</ul>

echo versus print

The two output constructs are easy to confuse. print is an expression that returns 1, while echo is a statement that returns nothing.

echo_print.php
<?php

declare(strict_types=1);

$result = print "hello\n";
echo "print returned ", $result, "\n";

The only difference that usually matters is the return value: print returns 1, so it can be used inside an expression, while echo cannot. Both are language constructs, and neither needs parentheses.

Featureechoprint
Argumentsone or moreexactly one
Return valuenone1
In an expressionnoyes
Parenthesesoptional for one valueoptional
$ php echo_print.php
hello
print returned 1

Best practices

Output is the boundary between the program and its users, so it deserves the same care as input.

Source

PHP manual: echo

PHP manual: print

PHP manual: strings

PHP manual: htmlspecialchars

This article covered the PHP echo keyword: the statement forms, several arguments, the parse errors around parentheses and assignment, type conversion, arrays and objects, HTML escaping, the short echo tag, and the difference from print. Continue with PHP print keyword for the expression form, or read PHP string interpolation and PHP string functions for the text side.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.