(PHP 4, PHP 5, PHP 7, PHP 8)
print — 输出字符串
说明
输出 expression
。
print
不是函数而是语言结构。它的参数是跟在 print
关键字后面的表达式,并且不用括号分割。
和 echo 最主要的区别是 print
仅接受一个参数,并始终返回 1
。
返回值
总是返回 1
。
示例
示例 #1 print 示例
<?php
print "print does not require parentheses.";
// 不会新增新行或者空格;下面会在一行中输出“helloworld”
print "hello";
print "world";
print "This string spans
multiple lines. The newlines will be
output as well";
print "This string spans\nmultiple lines. The newlines will be\noutput as well.";
// 参数可以是任何生成字符串的表达式
$foo = "example";
print "foo is $foo"; // foo is example
$fruits = ["lemon", "orange", "banana"];
print implode(" and ", $fruits); // lemon and orange and banana
// 即使使用了 declare(strict_types=1),非字符串表达式也会强制转换为字符串
print 6 * 7; // 42
// 因为 print 有返回值,所以可以在如下表达式中使用
// 以下输出“hello world”
if ( print "hello" ) {
echo " world";
}
// 以下输出“true”
( 1 === 1 ) ? print 'true' : print 'false';
?>
注释
注意: 使用括号
用括号括住
<?php
print "hello";
// 输出“hello”
print("hello");
// 也会输出“hello”,因为 ("hello") 是有效的表达式
print(1 + 2) * 3;
// 输出“9”;会首先对括号内的 1+2 进行求值,然后是 3*3
// print 语句会将整个表达式视为一个参数
if ( print("hello") && false ) {
print " - inside if";
}
else {
print " - inside else";
}
// 输出“ - inside if”
// 首先对表达式 ("hello") && false 求值, false
// 强制转换为空字符串“”且打印 print
// 结构,然后返回 1,所以运行 if 块中代码
?>当在大表达式中使用
<?php
if ( (print "hello") && false ) {
print " - inside if";
}
else {
print " - inside else";
}
// 输出“hello - inside else”
// 跟上个示例不同,首先对表达式 (print "hello") 求值
// 输出“hello”之后,print 返回 1
// 由于 1 && false 为 false,因此运行 else 块中代码
print "hello " && print "world";
// 输出“world1”;首先对 print "world" 求值,
// 然后表达式 "hello " && 1 传递给左侧的 print
(print "hello ") && (print "world");
// 输出“hello world”;括号强制 print 表达式
// 在 && 之前求值
?>
+添加备注
用户贡献的备注 3 notes
user at example dot net ¶
16 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
}
?>
danielxmorris @ gmail dotcom ¶
16 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) {
$_SERVER['SERVER_PROTOCOL'] ? print "$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
mark at manngo dot net ¶
1 year 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';
?>