PHP基础温习之echo print printf sprintf print_r var_dump的用法与区别
一、echo
echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误。而且echo是返回void的,并不返回值,所以不能使用它来赋值。
例子:
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">echo</span>("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 错误!不能用来赋值 </span><span style="color: #0000ff;">echo</span> "55nav"; <span style="color: #008000;">//</span><span style="color: #008000;"> 55nav </span><span style="color: #0000ff;">echo</span> ("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 55nav </span><span style="color: #0000ff;">echo</span> ("55nav","com"); <span style="color: #008000;">//</span><span style="color: #008000;">发生错误,有括号不能传递多个参数 </span><span style="color: #0000ff;">echo</span> "55nav"," com"," is", " web"; <span style="color: #008000;">//</span><span style="color: #008000;"> 不用括号的时候可以用逗号隔开多个值, 会输出 55nav com is web </span><span style="color: #0000ff;">echo</span> "55nav is 8 good 9 web."; <span style="color: #008000;">//</span><span style="color: #008000;"> 不管是否换行,最终显示都是为一行 55nav is good web. </span><span style="color: #800080;">$fistname</span>="55nav"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$fistname</span> com"; <span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $firstname = "55nav", 则会输出 55nav com. </span><span style="color: #0000ff;">echo</span> '$firstname com'; <span style="color: #008000;">//</span><span style="color: #008000;"> 由于使用单引号,所以不会输出$firstname的值,而是输出 $firstname com </span>?>
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">print</span>("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 这个是允许的 </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $a的值是1 </span>?>
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #800080;">$a</span>="55nav"<span style="color: #000000;">; </span><span style="color: #800080;">$c</span> = <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $c的值是TRUE </span> <span style="color: #800080;">$c</span> = <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $c的值是字符串55nav </span> ?>
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #008080;">printf</span>("My name is %s %s。","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> My name is 55nav com。 </span><span style="color: #008080;">printf</span>("My name is %1\$s %1\$s","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> 在s前添加1\$或2\$.....表示后面的参数显示的位置,此行输出 My name is 55nav 55nav因为只显示第一个参数两次。 </span><span style="color: #008080;">printf</span>("My name is %2\$s %1\$s","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> My name is com 55nav </span>?>
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #008080;">sprintf</span>("My name is %1\$s %1\$s","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;">你会发现没有任何东西输出的。 </span><span style="color: #800080;">$out</span> = <span style="color: #008080;">sprintf</span>("My name is %1\$s %2\$s","55nav", "com"<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$out</span>; <span style="color: #008000;">//</span><span style="color: #008000;">输出 My name is 55nav com </span>?>
复制代码代码如下:
<span style="color: #000000;">php </span><span style="color: #800080;">$a</span>=100<span style="color: #000000;">; </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$a</span>); <span style="color: #008000;">//</span><span style="color: #008000;">int(100) </span><span style="color: #800080;">$a</span>=100.356<span style="color: #000000;">; </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$a</span>); <span style="color: #008000;">//</span><span style="color: #008000;">float(100.356) </span>?>