echo
(PHP 4, PHP 5, PHP 7)
echo - 输出一个或多个字符串
描述
void echo ( string $arg1 [, string $... ] )
输出所有参数。不追加额外的换行符。
echo实际上不是一个函数(它是一种语言结构),所以你不需要使用圆括号。回声(与其他一些语言结构不同)不像函数那样工作,所以它不能总是用在函数的上下文中。此外,如果您想要传递多个参数来回显,则参数不能包含在括号内。
echo也有一个快捷方式的语法,在那里你可以立即跟随等号开头的标签。在PHP 5.4.0之前,这个简短语法仅适用于启用了short_open_tag配置设置。
I?have?<?=$foo?>?foo.
echo 的主要区别在于echo接受参数列表并且没有返回值。
参数
arg1
要输出的参数。
...
返回值
没有值返回。
例子
示例#1 回显示 例
<?php
echo?"Hello?World";
echo?"This?spans
multiple?lines.?The?newlines?will?be
output?as?well";
echo?"This?spans\nmultiple?lines.?The?newlines?will?be\noutput?as?well.";
echo?"Escaping?characters?is?done?\"Like?this\".";
//?You?can?use?variables?inside?of?an?echo?statement
$foo?=?"foobar";
$bar?=?"barbaz";
echo?"foo?is?$foo";?//?foo?is?foobar
//?You?can?also?use?arrays
$baz?=?array("value"?=>?"foo");
echo?"this?is?{$baz['value']}?!";?//?this?is?foo?!
//?Using?single?quotes?will?print?the?variable?name,?not?the?value
echo?'foo?is?$foo';?//?foo?is?$foo
//?If?you?are?not?using?any?other?characters,?you?can?just?echo?variables
echo?$foo;??????????//?foobar
echo?$foo,$bar;?????//?foobarbarbaz
//?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";
echo?<<<END
This?uses?the?"here?document"?syntax?to?output
multiple?lines?with?$variable?interpolation.?Note
that?the?here?document?terminator?must?appear?on?a
line?with?just?a?semicolon.?no?extra?whitespace!
END;
//?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:?",?1?+?2;
echo?"Hello?",?isset($name)???$name?:?"John?Doe",?"!";
通过连接,句点运算符比加法运算符和三元运算符具有更高的优先级,因此括号必须用于正确的行为:
<?php
echo?'Sum:?'?.?(1?+?2);
echo?'Hello?'?.?(isset($name)???$name?:?'John?Doe')?.?'!';
扩展内容
- print - 输出一个字符串
- printf() - 输出格式化的字符串
- flush() - 刷新系统输出缓冲区
- Heredoc语法
← crypt
explode →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com