PHP: print - Manual (original) (raw)
(PHP 4, PHP 5, PHP 7, PHP 8)
print — Output a string
Description
print is not a function but a language construct. Its argument is the expression following the print keyword, and is not delimited by parentheses.
The major differences to echo are thatprint only accepts a single argument and always returns1.
Parameters
expression
The expression to be output. Non-string values will be coerced to strings, even when thestrict_types directive is enabled.
Return Values
Returns 1, always.
Examples
Example #1 print examples
`<?php
print "print does not require parentheses.";
print PHP_EOL;// No newline or space is added; the below outputs "helloworld" all on one line
print "hello";
print "world";
print PHP_EOL;
"This string spans
multiple lines. The newlines will be
output as well";
print PHP_EOL;
"This string spans\nmultiple lines. The newlines will be\noutput as well.";
print PHP_EOL;// The argument can be any expression which produces a string
$foo = "example";
print "foo is $foo"; // foo is example
print PHP_EOL;$fruits = ["lemon", "orange", "banana"];
print implode(" and ", $fruits); // lemon and orange and banana
print PHP_EOL;// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
print 6 * 7; // 42
print PHP_EOL;// Because print has a return value, it can be used in expressions
// The following outputs "hello world"
if ( print "hello" ) {
echo " world";
}
print PHP_EOL;// The following outputs "true"
( 1 === 1 ) ? print 'true' : print 'false';
print PHP_EOL;
?>`
Notes
Note: Using with parentheses
Surrounding the argument to
<?php print "hello"; // outputs "hello"print("hello"); // also outputs "hello", because ("hello") is a valid expressionprint(1 + 2) * 3; // outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3 // the print statement sees the whole expression as one argumentif ( print("hello") && false ) { print " - inside if"; } else { print " - inside else"; } // outputs " - inside if" // the expression ("hello") && false is first evaluated, giving false // this is coerced to the empty string "" and printed // the print construct then returns 1, so code in the if block is run ?>When using
<?php if ( (print "hello") && false ) { print " - inside if"; } else { print " - inside else"; } // outputs "hello - inside else" // unlike the previous example, the expression (print "hello") is evaluated first // after outputting "hello", print returns 1 // since 1 && false is false, code in the else block is runprint "hello " && print "world"; // outputs "world1"; print "world" is evaluated first, // then the expression "hello " && 1 is passed to the left-hand print(print "hello ") && (print "world"); // outputs "hello world"; the parentheses force the print expressions // to be evaluated before the && ?>
Note: Because this is a language construct and not a function, it cannot be called usingvariable functions, or named arguments.
See Also
- echo - Output one or more strings
- printf() - Output a formatted string
- flush() - Flush system output buffer
- Ways to specify literal strings
Found A Problem?
17 years ago
Be careful when using print. Since print is a language construct and not a function, the parentheses around the argument is not required.
In fact, using parentheses can cause confusion with the syntax of a function and SHOULD be omited.
Most would expect the following behavior:
<?php
if (print("foo") && print("bar")) {
// "foo" and "bar" had been printed
}
?>
But since the parenthesis around the argument are not required, they are interpretet as part of the argument.
This means that the argument of the first print is
("foo") && print("bar")
and the argument of the second print is just
("bar")
For the expected behavior of the first example, you need to write:
<?php
if ((print "foo") && (print "bar")) {
// "foo" and "bar" had been printed
}
?>
2 years ago
The other major difference with echo is that print returns a value, even it’s always 1.
That might not look like much, but you can use print in another expression. Here are some examples:
<?php
rand(0,1) ? print 'Hello' : print 'goodbye';
print PHP_EOL;
print 'Hello ' and print 'goodbye';
print PHP_EOL;
rand(0,1) or print 'whatever';
?>
Here’s a more serious example:
<?php
function test() {
return !!rand(0,1);
}
test() or print 'failed';
?>danielxmorris @ gmail dotcom ¶
17 years ago
I wrote a println function that determines whether a \n or a <br /> should be appended to the line depending on whether it's being executed in a shell or a browser window. People have probably thought of this before but I thought I'd post it anyway - it may help a couple of people.
<?php
function println ($string_message) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mrow></mrow><mi>S</mi></msub><mi>E</mi><mi>R</mi><mi>V</mi><mi>E</mi><mi>R</mi><msup><mo stretchy="false">[</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>S</mi><mi>E</mi><mi>R</mi><mi>V</mi><mi>E</mi><msub><mi>R</mi><mi>P</mi></msub><mi>R</mi><mi>O</mi><mi>T</mi><mi>O</mi><mi>C</mi><mi>O</mi><msup><mi>L</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">]</mo><mo stretchy="false">?</mo><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">"</mi></mrow><annotation encoding="application/x-tex">_SERVER['SERVER_PROTOCOL'] ? print "</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord"><span></span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.05764em;">S</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.00773em;">ER</span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mord mathnormal" style="margin-right:0.00773em;">ER</span><span class="mopen"><span class="mopen">[</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.00773em;">SER</span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.0077em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.13889em;">P</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.02778em;">ROTOCO</span><span class="mord"><span class="mord mathnormal">L</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mclose">]?</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord">"</span></span></span></span>string_message<br />" : print "$string_message\n";
}
?>
Examples:
Running in a browser:
<?php println ("Hello, world!"); ?>
Output: Hello, world!<br />
Running in a shell:
<?php println ("Hello, world!"); ?>
Output: Hello, world!\n