The "print" keyword is used in PHP to output a string to the browser or other output stream. In this article, we will explore the syntax and usage of the
The PHP print Construct
print outputs one string to whatever output stream PHP is writing to — usually the browser when running a web page, or the terminal when running a script from the command line. Despite looking like a function, print is a language construct: it is built into the language itself, takes exactly one argument, and always returns the integer 1.
That single return value is what sets print apart from echo and makes it usable inside larger expressions. This chapter covers the syntax, how print differs from echo, when its return value matters, and the common ways it is used.
Syntax
print "Hello, world!";Because print is a construct and not a function, the parentheses are optional. Both forms are valid and behave identically:
print "Hello, world!"; // without parentheses
print("Hello, world!"); // with parenthesesThe parentheses simply group the argument; they do not turn print into a function call. It still accepts only one argument — you cannot pass a comma-separated list the way echo allows.
Outputting variables and expressions
print evaluates its argument first, so you can pass a variable, a concatenated string, or any expression that resolves to a string-like value. Variables inside double-quoted strings are interpolated automatically:
<?php
$name = "John";
// Interpolation inside a double-quoted string
print "Hello, $name!" . PHP_EOL; // Hello, John!
// Concatenation with the . operator
print "Sum: " . (1 + 2) . PHP_EOL; // Sum: 3PHP_EOL is the platform's newline character — handy in command-line scripts so each statement prints on its own line. (See PHP Operators for the . concatenation operator and PHP Variables for interpolation rules.)
Using the return value
Every call to print returns 1. On its own that looks useless, but it lets print appear where a value is expected — for example, inside an assignment or a logical expression:
<?php
// Assigning the result (always 1)
$result = print "Success" . PHP_EOL;
echo $result . PHP_EOL; // 1
// Using print inside a ternary expression
$loggedIn = false;
$loggedIn ? print "Welcome back" : print "Please log in";echo cannot be used this way: $x = echo "hi"; is a syntax error, because echo returns nothing. If you ever need output as part of an expression, reach for print.
print vs echo
The two are nearly interchangeable for plain output. The differences are small but worth knowing:
| Feature | print | echo |
|---|---|---|
| Return value | always 1 | none (cannot be used in expressions) |
| Number of arguments | exactly one | one or more (comma-separated) |
| Relative speed | slightly slower | slightly faster |
In practice both are fast enough that the speed gap never matters. Pick echo when you want to print several values at once; pick print when you need a return value. For a deeper side-by-side, see PHP echo vs print.
Printing HTML
Like echo, print simply writes whatever string it is given, so HTML markup passes straight through to the page:
<?php
$title = "Welcome";
print "<h1>$title</h1>";
print "<p>This paragraph was generated with <code>print</code>.</p>";The browser renders the result as ordinary HTML. Remember to escape any user-supplied data (for example with htmlspecialchars()) before printing it, to avoid cross-site scripting.
Summary
printis a language construct that outputs one string and always returns1.- Parentheses are optional —
print "x"andprint("x")are the same. - Its return value lets it be used inside expressions and assignments, unlike
echo. - Use
echofor multiple values; useprintwhen you need that return value. - Output (including HTML) is written verbatim, so escape untrusted data first.