print()
Our article is about the PHP function print(), which is used to output a string. This function is similar to the echo() function in PHP. In this article, we
print is a PHP language construct that outputs a string. It is one of the most common ways to send text to the browser or terminal, alongside echo. This page covers the syntax of print, the one trait that sets it apart from echo (its return value), how parentheses behave, and the gotchas to watch for.
A language construct — not a function — means print is part of the language grammar itself. In practice this is why you can write print "x" without parentheses, the same way you can with echo.
Syntax
int print(string $expression)print takes a single argument, $expression, which is converted to a string and written to the output. It always returns 1, so it can be used in larger expressions where echo cannot.
Because parentheses are optional, all of these are valid:
print "Hello, World!";
print("Hello, World!");
print "Sum is " . (2 + 3);A basic example
The output is:
Hello, World!print vs. echo
print and echo look almost identical, but there are two real differences:
echo | print | |
|---|---|---|
| Return value | none (returns nothing) | always 1 |
| Multiple arguments | yes — echo $a, $b; | no — one argument only |
| Speed | marginally faster | marginally slower |
The differences are tiny, so most projects pick one and stay consistent. Use echo when you want to output several values at once; reach for print only when you need a return value inside an expression. For a side-by-side comparison see echo and print in PHP.
<?php
// echo can take several arguments separated by commas
echo "a", "b", "c"; // abc
// print cannot — this is a syntax error:
// print "a", "b";
?>Using the return value
Because print returns 1, it can appear where an expression is expected. This is occasionally handy in a conditional or a ternary, where echo would not compile:
<?php
$loggedIn = true;
// Ternary: the print runs and evaluates to 1, but we only care about the side effect.
$loggedIn
? print("Welcome back!\n")
: print("Please sign in.\n");
?>Output:
Welcome back!This pattern is rare in real code — a plain if/else with echo is clearer — but it explains why print returns a value at all.
Printing variables and HTML
print outputs whatever string you build, including HTML. Interpolating variables inside a double-quoted string is the idiomatic way to mix text and data:
<?php
$user = "Ada";
print "<p>Hello, $user!</p>\n";
print 'Single quotes do NOT interpolate: $user' . "\n";
?>Output:
<p>Hello, Ada!</p>
Single quotes do NOT interpolate: $userNote the difference: double quotes ("...") expand $user, single quotes ('...') print it literally.
Common gotchas
printis not for arrays or objects. Passing an array prints the literal wordArrayand raises a notice. To inspect structured data useprint_r()orvar_dump()instead.- Output escapes nothing.
printwrites raw text. When echoing user input into HTML, wrap it inhtmlspecialchars()to avoid XSS. - For formatted output, prefer
printf. If you need padding, number formatting, or placeholders,printf()andsprintf()are the right tools.
Summary
print outputs a single string, always returns 1, and accepts optional parentheses. It is interchangeable with echo for everyday output; the only reason to prefer it is when you need its return value inside an expression. For debugging arrays and objects, switch to print_r() or var_dump(); for formatted strings, use printf().