echo

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

echo输出一个或多个字符串

说明

echo(string $arg1, string $... = ?): void

输出所有参数。不会换行。

echo 不是一个函数(它是一个语言结构), 因此你不一定要使用小括号来指明参数,单引号,双引号都可以。 echo (不像其他语言构造)不表现得像一个函数, 所以不能总是使用一个函数的上下文。 另外,如果你想给echo 传递多个参数, 那么就不能使用小括号。

echo 也有一个快捷用法,你可以在打开标记前直接用一个等号。在 PHP 5.4.0 之前,必须在php.ini 里面启用 short_open_tag 才有效。

I have <?=$foo?> foo.

print 最主要的不同之处是, echo 接受参数列表,并且没有返回值。

参数

arg1

要输出的参数

...

返回值

没有返回值。

范例

示例 #1 echo 例子

<?php
echo "Hello World";

// Strings can either be passed individually as multiple arguments or
// concatenated together and passed as a single argument
echo 'This ''string ''was ''made ''with multiple parameters.'chr(10);
echo 
'This ' 'string ' 'was ' 'made ' 'with concatenation.' "\n";

// Because echo does not behave like a function, the following code is invalid.
($some_var) ? echo 'true' : echo 'false';

// However, the following examples will work:
($some_var) ? print 'true' : print 'false'// print is also a construct, but
                                            // it behaves like a function, so
                                            // it may be used in this context.
echo $some_var 'true''false'// changing the statement around
?>

注释

注意: 因为是一个语言构造器而不是一个函数,不能被 可变函数 调用。

小技巧

相对 echo 中拼接字符串而言,传递多个参数比较好,考虑到了 PHP 中连接运算符(“.”)的优先级。 传入多个参数,不需要圆括号保证优先级:

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

如果拼接字符串,连接运算符(“.”)相对于加号优先级相同,比三元运算符优先级更高。为了正确,必须使用圆括号:

<?php
echo 'Sum: ' . (2);
echo 
'Hello ' . (isset($name) ? $name 'John Doe') . '!';

参见

add a noteadd a note

User Contributed Notes 4 notes

up
30
pemapmodder1970 at gmail dot com
5 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
1
retrobytespr at mail dot com
4 months 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;
up
-3
t3tesla at gmail dot com
11 months 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
-17
mparsa1372 at gmail dot com
1 year ago
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):

<?php
echo "<h2>PHP is Fun!</h2>";
echo
"Hello world!<br>";
echo
"I'm about to learn PHP!<br>";
echo
"This ", "string ", "was ", "made ", "with multiple parameters.";
?>

备份地址:http://www.lvesu.com/blog/php/function.echo.php