Home > Article > Backend Development > Basic PHP review: echo print printf sprintf print_r var_dump usage and difference, print_rvar_dump_PHP tutorial
<?<span>php </span><span>$a</span> = <span>echo</span>("55nav"); <span>//</span><span> 错误!不能用来赋值 </span> <span>echo</span> "55nav"; <span>//</span><span> 55nav </span> <span>echo</span> ("55nav"); <span>//</span><span> 55nav </span> <span>echo</span> ("55nav","com"); <span>//</span><span>发生错误,有括号不能传递多个参数 </span> <span>echo</span> "55nav"," com"," is", " web"; <span>//</span><span> 不用括号的时候可以用逗号隔开多个值, 会输出 55nav com is web </span> <span>echo</span> "55nav is 8 good 9 web."; <span>//</span><span> 不管是否换行,最终显示都是为一行 55nav is good web. </span> <span>$fistname</span>="55nav"<span>; </span><span>echo</span> "<span>$fistname</span> com"; <span>//</span><span> 如果 $firstname = "55nav", 则会输出 55nav com. </span> <span>echo</span> '$firstname com'; <span>//</span><span> 由于使用单引号,所以不会输出$firstname的值,而是输出 $firstname com </span> ?>
<?<span>php </span><span>$a</span> = <span>print</span>("55nav"); <span>//</span><span> 这个是允许的 </span> <span>echo</span> <span>$a</span>; <span>//</span><span> $a的值是1 </span> ?>
<?<span>php </span><span>$a</span>="55nav"<span>; </span><span>$c</span> = <span>print_r</span>(<span>$a</span><span>); </span><span>echo</span> <span>$c</span>; <span>//</span><span> $c的值是TRUE </span> <span>$c</span> = <span>print_r</span>(<span>$a</span>,<span>true</span><span>); </span><span>echo</span> <span>$c</span>; <span>//</span><span> $c的值是字符串55nav </span> ?>
<?<span>php </span><span>printf</span>("My name is %s %s。","55nav", "com"); <span>//</span><span> My name is 55nav com。 </span> <span>printf</span>("My name is %1\$s %1\$s","55nav", "com"); <span>//</span><span> 在s前添加1\$或2\$.....表示后面的参数显示的位置,此行输出 My name is 55nav 55nav因为只显示第一个参数两次。 </span> <span>printf</span>("My name is %2\$s %1\$s","55nav", "com"); <span>//</span><span> My name is com 55nav </span> ?>
<?<span>php </span><span>sprintf</span>("My name is %1\$s %1\$s","55nav", "com"); <span>//</span><span>你会发现没有任何东西输出的。 </span> <span>$out</span> = <span>sprintf</span>("My name is %1\$s %2\$s","55nav", "com"<span>); </span><span>echo</span> <span>$out</span>; <span>//</span><span>输出 My name is 55nav com </span> ?>
<?<span>php </span><span>$a</span>=100<span>; </span><span>var_dump</span>(<span>$a</span>); <span>//</span><span>int(100) </span> <span>$a</span>=100.356<span>; </span><span>var_dump</span>(<span>$a</span>); <span>//</span><span>float(100.356) </span> ?>