PHP 8.3.4 Released!

echo

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

echo输出一个或多个字符串

说明

echo(string ...$expressions): void

输出一个或多个表达式,没有额外的换行符或者空格。

echo 不是函数,而是语言结构。它的参数是表达式列表,跟在 echo 关键字后面,用逗号分隔,不用括号分隔。与其它的返回结构不同,echo 没有返回值,因为不能在表达式的上下文中使用。

echo 也有快捷语法,可以在开始标记后直接跟等号。即使禁用了 short_open_tag 配置,此语法也可用。

I have <?=$foo?> foo.

print 的主要区别是 echo 接受多个参数且没有返回值。

参数

expressions

要输出的一个或多个字符串表达式,用逗号分隔。即使启用 strict_types 指令,非字符串值也会强制转换为字符串。

返回值

没有返回值。

示例

示例 #1 echo 示例

<?php
echo "echo does not require parentheses.";

// 字符串可以作为多个参数单独传递,
// 也可以连接在一起作为单个参数传递
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', "\n";
echo
'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

// 不会有新行或者空格;下面将会在一行中输出“helloworld”
echo "hello";
echo
"world";

// 跟上面一样
echo "hello", "world";

echo
"This string spans
multiple lines. The newlines will be
output as well"
;

echo
"This string spans\nmultiple lines. The newlines will be\noutput as well.";

// 参数是可以产生字符串的任意表达式
$foo = "example";
echo
"foo is $foo"; // foo is example

$fruits = ["lemon", "orange", "banana"];
echo
implode(" and ", $fruits); // lemon and orange and banana

// 即使使用 declare(strict_types=1),非字符串表达式也会强制转换字符串
echo 6 * 7; // 42

// 因为 echo 的表现跟表达式不同,所以下面的代码无效。
($some_var) ? echo 'true' : echo 'false';

// 但是,下面的示例又正常:
($some_var) ? print 'true' : print 'false'; // print 也是语言结构,
// 但它是有效的表达式,返回 1,
// 所以可以在此上下文中使用。

echo $some_var ? 'true': 'false'; // 首先运行表达式然后传递它到 echo
?>

注释

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

注意: 使用括号

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

<?php
echo "hello";
// 输出“hello”

echo("hello");
// 也会输出“hello”,因为 ("hello") 是有效表达式

echo(1 + 2) * 3;
// 输出“9”;首先对括号内的 1+2 求值,然后 echo 语句将
// 整个表达式 3*3 视为一个参数

echo "hello", " world";
// 输出“hello world”

echo("hello"), (" world");
// 输出“hello world”;括号是每个表达式的一部分

echo("hello", " world");
// 抛出解析错误,因为 ("hello", " world") 不是有效的表达式
?>

小技巧

将多个参数传递给 echo 可以避免 PHP 中连接运算符中优先级引起的复杂性。例如,连接运算符的优先级高于三元运算符,在 PHP 8.0.0 之前与加减法具有相同的优先级。

<?php
// 下面的表达式 'Hello ' . isset($name) 将首先求值,
// 因为始终为 true,所以 echo 的参数始终是 $name
echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';

// 预期的行为需要额外的括号
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';

// 在 PHP 8.0.0 之前,下面会输出“2”,而不是“Sum: 3”
echo 'Sum: ' . 1 + 2;

// 再次添加括号以确保期望的求值顺序
echo 'Sum: ' . (1 + 2);

如果传递多个参数,则不需要括号来强制执行优先级,因为每个表达式都是独立的:

<?php
echo "Hello ", isset($name) ? $name : "John Doe", "!";

echo
"Sum: ", 1 + 2;

参见

add a note

User Contributed Notes 3 notes

up
34
pemapmodder1970 at gmail dot com
6 years ago
Passing multiple parameters to echo using commas (',')is not exactly identical to using the concatenation operator ('.'). There are two notable differences.

First, concatenation operators have much higher precedence. Referring to http://php.net/operators.precedence, there are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings.

<?php
echo "The sum is " . 1 | 2; // output: "2". Parentheses needed.
echo "The sum is ", 1 | 2; // output: "The sum is 3". Fine.
?>

Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one.

<?php
function f($arg){
var_dump($arg);
return
$arg;
}
echo
"Foo" . f("bar") . "Foo";
echo
"\n\n";
echo
"Foo", f("bar"), "Foo";
?>

The output would be:
string(3) "bar"FoobarFoo

Foostring(3) "bar"
barFoo

It would become a confusing bug for a script that uses blocking functions like sleep() as parameters:

<?php
while(true){
echo
"Loop start!\n", sleep(1);
}
?>

vs

<?php
while(true){
echo
"Loop started!\n" . sleep(1);
}
?>

With ',' the cursor stops at the beginning every newline, while with '.' the cursor stops after the 0 in the beginning every line (because sleep() returns 0).
up
-7
t3tesla at gmail dot com
2 years ago
We can use the 'echo' shortcut syntax with the conditional operator (expr1) ? (expr2) : (expr3)

<?php
$some_var
= 10;
?>
Back to html :
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give : <p class="class1">Some text.</p>

<?php
$some_var
= 4;
?>
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give : <p class="class2">Some text.</p>
up
-10
retrobytespr at mail dot com
2 years ago
If you have a large block of text, say your blog or something includes code examples, you may use the <<< operator (?) to define the start and end of your block to be echoed out. For instance:

<?php
echo <<< JAVASCRIPT

function convertTroyOuncesToGrams(troyOunce) {
return troyOunce / 31.1034768;
}

JAVASCRIPT; # End of block

?>

You may also embed PHP strings and other simple scalars into your blocks of text, for example:

<?php
$troyOunceAsGrams
= 31.1034768;

echo <<< JAVASCRIPT

function convertTroyOuncesToGrams(troyOunce) {
return troyOunce /
{$troyOunceAsGrams};
}

JAVASCRIPT;
To Top