Maison >développement back-end >tutoriel php >PHP函数echo()的定义和用法解析_PHP教程
在使用定义和用法
PHP函数echo()输出一个或多个字符串。
语法
echo(strings)
参数 描述
strings 必需。一个或多个要发送到输出的字符串。
提示和注释
注释:echo() 实际上不是一个函数,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个或多个参数,那么使用括号会发生解析错误。
提示:echo() 函数比 print() 函数快一点点。
提示:PHP函数echo()可以使用简化语法。参见以下示例。
<ol class="dp-xml"><li class="alt"><span><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span> </span><span class="tag"><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></li></span><li class="alt"><span><span class="tag"><span class="tag"></span><span> </span><span class="tag"><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></li></span></li> <li class="alt"><span><span class="tag"><span class="tag"><span class="tag"><span class="tag"><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 class="tag"><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 class="tag"><span class="tag-name">html</span><span class="tag">></span><span> </span></span></span></span></span></span></span></span></li></span></li></span></li></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> <li><span> echo 123adb; //输出正确 </span></li> <li class="alt"><span> echo 123abd; //正确 </span></li> <li><span> echo "123adb"; //正确 </span></li> <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> <li> <span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">"123456"</span><span>; </span> </li> <li class="alt"><span>echo $a; //正确 </span></li> <li> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li class="alt"><span>echo "$a"; //正确 </span></li> <li> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li class="alt"><span>echo $a;//不正确 </span></li> <li> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li class="alt"><span>echo abcdef; //也能正确的显示,但是强烈不建议这么做,<br>因为有很多特殊符号这么做不能正确的显示。 </span></li> <li> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li class="alt"> <span>//echo </span><span class="tag"><span class="tag-name">abd</span><span class="tag">></span><span>; //比如说这样,带了尖括的,就不能用,<br>你可以去了前面的注释试试会不会报错。 </span><li> <span>echo "</span><span class="tag"><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> <li><span>echo $b+10; //显示 20 </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li><span>echo "$b+10"; </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li><span>echo "结果是:"."$b"; //这两种写法的效果是一样的。 </span></li> <li class="alt"> <span>echo "</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>"; </span><li><span>echo "结果是:$b"; //这两种写法的效果是一样的。 </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li></span> </li></span> </li></span> </li></span> </li></span> </li></span> </li></span> </li></span> </li></span> </li> </ol>
echo ""和PHP函数echo()是有区别的,加双引号时会对变量与特定的符号进行转义,加单引号时不会进行转义,会将单引号内的符号直接的输出,所以说用单引号比用双引号时更快一点。能不用双引号时就不要用了吧。