Heim >Backend-Entwicklung >PHP-Tutorial >PHP打印函数的种类总结_PHP教程
在PHP打印函数1 echo()
可以同时输出多个字符串,可以多个参数,并不需要圆括号,无返回值。
PHP打印函数2 print()
只可以同时输出一个字符串,一个参数,需要圆括号,有返回值,当其执行失败时返flase .print 的用法和C语言很像,所以会对输出内容里的%做特殊解释。
PHP打印函数3 die();
有两个功能:先输出内容,然后退出程序。(常用在链接服务器,数据库)
mysql_connect("locahost","root","root") or die("链接服务器失败!");
PHP打印函数4 printf();
printf("参数1",参数2):参数1=按什么格式输出;参数2=输出的变量。($s:按字符串;$d:按整型;$b:按二进制;$x:按16进制;$o:按八进制; $f:按浮点型)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">num</span><span>=</span><span class="attribute-value">100</span><span>.001; </span> </li> <li class="alt"><span>printf("%d",$num); //100 </span></li> <li><span>printf("%s",$num); //100.001 </span></li> <li class="alt"><span>printf("%s---%d---%b---%x---%o---%f",$num,$num,$num,$num,$num,$num) </span></li> <li><span>//100.001---100---1100100---64---144---1001.00100 </span></li> <li class="alt"><span>printf("%.2f",$num); //100.00 (小数点保留2位) </span></li> <li><span>printf("%.1f",$num); //100.0 (小数点保留1位) </span></li> <li class="alt"><span>printf("%`#10s",$num); //###100.001 </span></li> <li><span>printf("%`@10s",$num); //@@@100.001 </span></li> <li class="alt"><span>printf("%`_10s",$num); //___100.001 </span></li> <li><span>printf("%`#-10s",$num); //100.001### </span></li> <li class="alt"><span>printf("%`@-10s",$num); //100.001@@@ </span></li> <li><span>printf("%`_-10s",$num); //100.001___ </span></li> <li class="alt"><span>printf("%`#20s",$num); //#############100.001 </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
PHP打印函数5 sprintf();
此并不能直接输出,先赋给一个变量,然后再输出变量。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">num</span><span>=</span><span class="attribute-value">100</span><span>.001; </span> </li> <li class="alt"> <span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">sprintf</span><span>("%d",$num); </span> </li> <li><span>echo $a; //100 </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
PHP打印函数6 print_r();
功能:只用于输出数组。
PHP打印函数7 var_dump();
功能: 输出变量的内容,类型或字符串的内容,类型,长度。常用来调试。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">100</span><span>; </span> </li> <li class="alt"><span>var_dump($a); //int(3) 100 </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>