PHP 8.3.4 Released!

print

(PHP 4, PHP 5, PHP 7, PHP 8)

print输出字符串

说明

print(string $expression): int

输出 expression

print 不是函数而是语言结构。它的参数是跟在 print 关键字后面的表达式,并且不用括号分割。

echo 最主要的区别是 print 仅接受一个参数,并始终返回 1

参数

expression

要输出的表达式。即使启用 strict_types 指令,非字符串也会强制转换为字符串。

返回值

总是返回 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';
?>

注释

注意: 使用括号

用括号括住 print 的参数并不会引发语法错误,而且会产生看起来像是普通函数调用的语法。然而,这可能会产生误导,因为括号实际上是输出表达式的一部分,而非 print 语法本身的一部分。

<?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 块中代码
?>

当在大表达式中使用 print 时,需要将关键字及其参数放在括号中以便得出预期的结果:

<?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 表达式
// 在 && 之前求值
?>

注意: 因为是语言构造器而不是函数,不能被 可变函数 或者 命名参数 调用。

参见

add a note

User Contributed Notes 3 notes

up
32
user at example dot net
15 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
}
?>
up
12
danielxmorris @ gmail dotcom
15 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
up
2
mark at manngo dot net
6 months 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';
?>
To Top