在使用定义和用法
PHP函数echo()输出一个或多个字符串。
语法
echo(strings)
参数 描述
立即学习“PHP免费学习笔记(深入)”;
strings 必需。一个或多个要发送到输出的字符串。
提示和注释
注释:echo() 实际上不是一个函数,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个或多个参数,那么使用括号会发生解析错误。
提示:echo() 函数比 print() 函数快一点点。
提示:PHP函数echo()可以使用简化语法。参见以下示例。
<OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN><SPAN><?</SPAN><SPAN class=tag><?</span><SPAN class=tag-name>php</SPAN><SPAN> $</SPAN><SPAN class=attribute>color</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>"red"</SPAN><SPAN>; </SPAN><SPAN class=tag>?></SPAN></SPAN></SPAN><LI class=alt><SPAN><SPAN class=tag><SPAN class=tag></SPAN><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>></SPAN><SPAN>Roses are </SPAN></SPAN></SPAN><LI class=alt><SPAN><SPAN class=tag><SPAN><?</SPAN><SPAN class=tag><?</span><SPAN>=$color</SPAN><SPAN class=tag>?></SPAN></SPAN></SPAN></SPAN><LI class=alt><SPAN><SPAN class=tag><SPAN class=tag><SPAN class=tag></< SPAN><SPAN class=tag></< span><SPAN class=tag-name>p</SPAN><SPAN class=tag>></SPAN></SPAN></SPAN></SPAN></SPAN><LI class=alt><SPAN><SPAN class=tag><SPAN class=tag><SPAN class=tag><SPAN class=tag></SPAN><SPAN> </< SPAN><SPAN class=tag></< span><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN></SPAN></SPAN></SPAN></SPAN><LI class=alt><SPAN><SPAN class=tag><SPAN class=tag><SPAN class=tag><SPAN class=tag><SPAN></< SPAN><SPAN class=tag></< span><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN></LI></OL>
1.echo 输出标量
<OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><?</span><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI><SPAN> echo 123adb; //输出正确 </SPAN><LI class=alt><SPAN> echo 123abd; //正确 </SPAN><LI><SPAN> echo "123adb"; //正确 </SPAN><LI class=alt><SPAN class=tag>?></SPAN><SPAN> </SPAN></LI></OL>
2、PHP函数echo()输出变量,常量
<OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><?</span><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI><SPAN>$</SPAN><SPAN class=attribute>a</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"123456"</SPAN><SPAN>; </SPAN><LI class=alt><SPAN>echo $a; //正确 </SPAN><LI><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI class=alt><SPAN>echo "$a"; //正确 </SPAN><LI><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI class=alt><SPAN>echo $a;//不正确 </SPAN><LI><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI class=alt><SPAN>echo abcdef; //也能正确的显示,但是强烈不建议这么做,<br>因为有很多特殊符号这么做不能正确的显示。 </SPAN><LI><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI class=alt><SPAN>//echo </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>abd</SPAN><SPAN class=tag>></SPAN><SPAN>; //比如说这样,带了尖括的,就不能用,<br>你可以去了前面的注释试试会不会报错。 </SPAN><LI><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>b</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>10</SPAN><SPAN>; </SPAN><LI><SPAN>echo $b+10; //显示 20 </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI><SPAN>echo "$b+10"; </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI><SPAN>echo "结果是:"."$b"; //这两种写法的效果是一样的。 </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag></< span><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>"; </SPAN><LI><SPAN>echo "结果是:$b"; //这两种写法的效果是一样的。 </SPAN><LI class=alt><SPAN class=tag>?></SPAN><SPAN> </SPAN></LI></OL>
echo ""和PHP函数echo()是有区别的,加双引号时会对变量与特定的符号进行转义,加单引号时不会进行转义,会将单引号内的符号直接的输出,所以说用单引号比用双引号时更快一点。能不用双引号时就不要用了吧。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号