1
0
mirror of https://github.com/php/doc-zh.git synced 2026-04-28 10:43:14 +02:00
Files
2025-06-30 23:06:39 +08:00

254 lines
7.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 45042fef652f1b4e904e809fcbfcf31f6c60670b Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook" xml:id="function.echo">
<refnamediv>
<refname>echo</refname>
<refpurpose>输出一个或多个字符串</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>void</type><methodname>echo</methodname>
<methodparam rep="repeat"><type>string</type><parameter>expressions</parameter></methodparam>
</methodsynopsis>
<simpara>
输出一个或多个表达式,没有额外的换行符或者空格。
</simpara>
<para>
<literal>echo</literal> 不是函数,而是语言结构。它的参数是表达式列表,跟在 <literal>echo</literal>
关键字后面,用逗号分隔,不用括号分隔。与其它的返回结构不同,<literal>echo</literal>
没有返回值,因为不能在表达式的上下文中使用。
</para>
<para>
<literal>echo</literal> 也有快捷语法,可以在开始标记后直接跟等号。即使禁用了 <link
linkend="ini.short-open-tag">short_open_tag</link> 配置,此语法也可用。
<informalexample>
<programlisting role="php">
<![CDATA[
I have <?=$foo?> foo.
]]>
</programlisting>
</informalexample>
</para>
<para>
<function>print</function> 的主要区别是 <literal>echo</literal> 接受多个参数且没有返回值。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>expressions</parameter></term>
<listitem>
<para>
要输出的一个或多个字符串表达式,用逗号分隔。即使启用 <link
linkend="language.types.declarations.strict"><literal>strict_types</literal>
指令</link>,非字符串值也会强制转换为字符串。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><literal>echo</literal> 示例</title>
<programlisting role="php">
<![CDATA[
<?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
// 但是,下面的示例又正常:
($some_var) ? print 'true' : print 'false'; // print 也是语言结构,
// 但它是有效的表达式,返回 1,
// 所以可以在此上下文中使用。
echo $some_var ? 'true': 'false'; // 首先运行表达式然后传递它到 echo
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><literal>echo</literal> 不是表达式</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
// 因为 echo 的行为与表达式不同,所以下面的代码无效。
($some_var) ? echo 'true' : echo 'false';
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&note.language-construct;
<note>
<title>使用括号</title>
<para>
使用括号括住 <literal>echo</literal>
后的单个参数并不会引发语法错误,而且还会产生看起来像普通函数调用的语法。但是,这可能会产生误导,因为括号实际上是输出表达式的一部分,而不是
<literal>echo</literal> 语法本身的一部分。
<example>
<title>使用括号</title>
<programlisting role="php">
<![CDATA[
<?php
echo "hello", PHP_EOL;
// 输出“hello”
echo("hello"), PHP_EOL;
// 也会输出“hello”,因为 ("hello") 是有效表达式
echo(1 + 2) * 3, PHP_EOL;
// 输出“9”;首先对括号内的 1+2 求值,然后 echo 语句将
// 整个表达式 3*3 视为一个参数
echo "hello", " world", PHP_EOL;
// 输出“hello world”
echo("hello"), (" world"), PHP_EOL;
// 输出“hello world”;括号是每个表达式的一部分
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>无效表达式</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
echo("hello", " world"), PHP_EOL;
// 抛出解析错误,因为 ("hello", " world") 不是有效的表达式
?>
]]>
</programlisting>
</example>
</para>
</note>
<tip>
<para>
将多个参数传递给 <literal>echo</literal> 可以避免 PHP
中连接运算符中优先级引起的复杂性。例如,连接运算符的优先级高于三元运算符,在
PHP 8.0.0 之前与加减法具有相同的优先级。
</para>
<programlisting role="php">
<![CDATA[
<?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);
]]>
</programlisting>
<para>
如果传递多个参数,则不需要括号来强制执行优先级,因为每个表达式都是独立的:
</para>
<programlisting role="php">
<![CDATA[
<?php
echo "Hello ", isset($name) ? $name : "John Doe", "!";
echo "Sum: ", 1 + 2;
]]>
</programlisting>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>print</function></member>
<member><function>printf</function></member>
<member><function>flush</function></member>
<member><link linkend="language.types.string">指定文本字符串的方式</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->