ZetCode

PHP print Statement

last modified November 25, 2026

The PHP print construct writes one value to the output and returns 1. It looks like a function, but it is a language construct with its own precedence and a single argument.

This article shows how print handles variables, expressions, and values of different types, how it differs from echo, and how to escape output safely.

Basic definitions

print is a language construct that writes one value to the output. It is not a function, so it cannot be called through a variable function, and its argument needs no parentheses unless the expression requires grouping.

The construct always returns the integer 1, which makes it usable inside an expression. echo produces the same output but accepts several arguments and returns nothing, so it cannot be used as a value. Both write to standard output in the CLI and to the response body on a web server.

print is unrelated to print_r, which renders arrays and objects for debugging, and to printf, which formats its arguments. The name is the only thing they share.

Printing a string

The first example prints one interpolated string.

print_basic.php
<?php

declare(strict_types=1);

$name = "Ada";

print "Hello there, $name!\n";

The double-quoted string substitutes $name and interprets \n as a newline. The argument is a single expression, here a literal string; the construct writes it and discards the returned 1.

$ php print_basic.php
Hello there, Ada!

Variables and expressions

Inside double quotes, a variable is replaced by its value. An expression needs braces or concatenation.

print_vars.php
<?php

declare(strict_types=1);

$item = "coffee";
$price = 4.5;
$quantity = 3;

print "Item: $item\n";
print "Price: {$price} EUR\n";
print "Total: " . ($price * $quantity) . " EUR\n";

Braces around {$price} delimit the variable name, which matters when text follows it directly. The total is computed outside the string and joined with the dot operator. Single quotes would print the variable names literally, because they do not interpolate.

$ php print_vars.php
Item: coffee
Price: 4.5 EUR
Total: 13.5 EUR

print as an expression

Because print returns 1, it can stand where an expression is expected.

print_expr.php
<?php

declare(strict_types=1);

$name = "";
$name ?: print "anonymous\n";

$written = print "message\n";

echo "print returned $written\n";

The short ternary $name ?: print ... prints only when $name is empty; in that case the expression takes the value 1 returned by print. The assignment $written = print "message" stores that 1 and prints the message as a side effect. echo cannot be used in either position. The construct has low precedence, so wrap it in parentheses when it is combined with other operators.

$ php print_expr.php
anonymous
message
print returned 1

One argument only

echo accepts a comma-separated list; print accepts exactly one expression. Multiple values must be concatenated or formatted first.

print_args.php
<?php

declare(strict_types=1);

echo "echo", " accepts", " several", " arguments\n";

print "print accepts one; concatenate: " . 2 . " + " . 3 . "\n";

$separator = ", ";
print "a" . $separator . "b" . "\n";

The echo statement writes four pieces without building an intermediate string. The print statement joins its parts with the dot operator. Writing print "a", "b"; is a parse error, because the comma is not an operator that print understands. For several values, echo or a formatting function is usually clearer.

$ php print_args.php
echo accepts several arguments
print accepts one; concatenate: 2 + 3
a, b

Values of other types

print converts its argument to a string. The result depends on the type, and objects need a conversion method.

print_types.php
<?php

declare(strict_types=1);

print "true: " . true . "\n";
print "false: " . false . "|\n";
print "null: " . null . "|\n";
print "int: " . 42 . "\n";
print "float: " . 1.5 . "\n";

class Money
{
    public function __construct(private int $cents)
    {
    }

    public function __toString(): string
    {
        return number_format($this->cents / 100, 2) . " EUR";
    }
}

print "object: " . new Money(1250) . "\n";

try {
    print new stdClass();
} catch (Error $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

true becomes 1, while false and null become empty strings, so the bars in the output mark their positions. Integers and floats use their usual string form. An object is converted through __toString; without that method, PHP throws an Error, which the example catches to keep the script running. An array is not convertible either: print writes Array and raises an Array to string conversion warning, so use implode or print_r for arrays.

$ php print_types.php
true: 1
false: |
null: |
int: 42
float: 1.5
object: 12.50 EUR
Error: Object of class stdClass could not be converted to string

Escaping output

When the output is HTML, data that comes from a user must be escaped before it is printed.

print_escape.php
<?php

declare(strict_types=1);

$comment = "<script>alert('x')</script>";

print htmlspecialchars($comment, ENT_QUOTES, "UTF-8") . "\n";

htmlspecialchars turns the characters that are special in HTML, including <, >, &, and quotes, into entities, so the browser displays the script text instead of executing it. ENT_QUOTES also encodes single quotes, and the explicit UTF-8 avoids depending on the default charset. The same rule applies to echo; the construct used for output does not affect safety.

$ php print_escape.php
&lt;script&gt;alert(&#039;x&#039;)&lt;/script&gt;

print versus echo

The two constructs differ in three practical points.

printecho
Argumentsexactly oneone or more
Return value1none
Usable in an expressionyesno

Both accept an optional pair of parentheses, and both are language constructs rather than functions. echo is the usual choice for plain output; print is useful in the rare expression that needs the returned 1. Any speed difference between them is irrelevant compared with the cost of the surrounding code.

Best practices

The construct is simple, so most of the advice concerns output safety and consistency.

Source

PHP manual: print

PHP manual: echo

This article covered the PHP print construct: its single argument, its return value, its use in expressions, string conversion, escaping, and the differences from echo. Continue with PHP echo keyword for the multi-argument alternative and PHP printf and sprintf for formatted output.

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.