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.
print "Hello\n"; $result = print "Hi\n"; // $result is 1
Try it
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.
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
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. Useechofor comma-separated arguments. - Precedence.
printtakes everything to its right as its argument, soprint "a" . "b"printsab, andprint 1 + 2prints3. Inside a larger expression you may need parentheses. - Not a function. You cannot pass
printas a callback or use it with named arguments. Useprintf()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 anifstatement. - Output buffering applies exactly as for
echo. - Debug output. For arrays and objects use
print_r()orvar_dump(), notprint. - Style. Most PHP code uses
echo; consistency matters more than the small differences.