Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP print statement

Reference for PHP print: output a single string, why it always returns 1, using it inside expressions, and the differences from echo.

print outputs a single value and always returns the integer 1. Like echo it is a language construct, so parentheses are optional. Because it returns a value, print can be used where an expression is expected, which is its only practical advantage over echo.

The argument is converted to a string using the same rules as echo. To output several values you must concatenate them with . or use several print statements.

PHP
print "Hello\n";
$result = print "Hi\n";   // $result is 1

Try it

PHP print returns 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP print in expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
print expression;
int print(expression)   // conceptual: print behaves like this
Name Description
expression A single value. It is converted to a string and written to the output.

Return value: always 1.

Errors: arrays produce an "Array to string conversion" warning; an object without __toString() throws an Error.

Usage notes

  • One argument only. print "a", "b"; is a syntax error. Use echo for comma-separated arguments.
  • Precedence. print takes everything to its right as its argument, so print "a" . "b" prints ab, and print 1 + 2 prints 3. Inside a larger expression you may need parentheses.
  • Not a function. You cannot pass print as a callback or use it with named arguments. Use printf() or a closure instead.
  • Return value is rarely useful. It exists so you can write short-circuit tricks like $ok and print "...", which are usually clearer as an if statement.
  • Output buffering applies exactly as for echo.
  • Debug output. For arrays and objects use print_r() or var_dump(), not print.
  • Style. Most PHP code uses echo; consistency matters more than the small differences.