欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php輸出echo、print、print_r、printf、sprintf、var_dump的區(qū)別比較

 更新時(shí)間:2013年06月21日 10:25:30   作者:  
本篇文章是對(duì)php輸出echo、print、print_r、printf、sprintf、var_dump的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
用.net開發(fā)已經(jīng)5年了,最近突然想接觸一下。net以外的東西,于是乎就來(lái)看看php了。在學(xué)習(xí)php中首先看看幾個(gè)輸出函數(shù)。
一、echo
echo() 實(shí)際上不是一個(gè)函數(shù),是php語(yǔ)句,因此您無(wú)需對(duì)其使用括號(hào)。不過(guò),如果您希望向 echo() 傳遞一個(gè)以上的參數(shù),那么使用括號(hào)會(huì)發(fā)生解析錯(cuò)誤。而且echo是返回void的,并不返回值,所以不能使用它來(lái)賦值。
例子:
復(fù)制代碼 代碼如下:

<?php 
$a = echo("55nav"); // 錯(cuò)誤!不能用來(lái)賦值 
echo "55nav"; // 55nav  
echo ("55nav"); // 55nav  
echo ("55nav","com"); //發(fā)生錯(cuò)誤,有括號(hào)不能傳遞多個(gè)參數(shù)  
echo "55nav"," com"," is", " web";  // 不用括號(hào)的時(shí)候可以用逗號(hào)隔開多個(gè)值, 會(huì)輸出 55nav com is web  
echo "55nav is  8 good  9 web.";  // 不管是否換行,最終顯示都是為一行 55nav is good web. 
$fistname="55nav";
echo "$fistname com"; // 如果 $firstname = "55nav", 則會(huì)輸出 55nav com. 
echo '$firstname com'; // 由于使用單引號(hào),所以不會(huì)輸出$firstname的值,而是輸出 $firstname com
?>

二、print
print() 和 echo() 用法一樣,但是echo速度會(huì)比print快一點(diǎn)點(diǎn)。實(shí)際上它也不是一個(gè)函數(shù),因此您無(wú)需對(duì)其使用括號(hào)。不過(guò),如果您希望向print() 傳遞一個(gè)以上的參數(shù),那么使用括號(hào)會(huì)發(fā)生解析錯(cuò)誤。注意print總是返回1的,這個(gè)和echo不一樣,也就是可以使用print來(lái)賦值,不過(guò)沒有實(shí)際意義。
例子:
復(fù)制代碼 代碼如下:

<?php
$a = print("55nav"); // 這個(gè)是允許的 
echo $a; // $a的值是1
?>

三、print_r 函數(shù)
print_r函數(shù)打印關(guān)于變量的易于理解的信息。
語(yǔ)法:mixed print_r ( mixed $expression [, bool return ] )
如果變量是string , integer or float , 將會(huì)直接輸出其值,如果變量是一個(gè)數(shù)組,則會(huì)輸出一個(gè)格式化后的數(shù)組,便于閱讀,也就是有key和value對(duì)應(yīng)的那種格式。對(duì)于object對(duì)象類同。print_r有兩個(gè)參數(shù),第一個(gè)是變量,第二個(gè)可設(shè)為true,如果設(shè)為true,則會(huì)返回字符串,否則返回布爾值TRUE。
例子:
復(fù)制代碼 代碼如下:

<?php
 $a="55nav";
 $c = print_r($a); 
 echo $c;  // $c的值是TRUE 
 $c = print_r($a,true); 
 echo $c; // $c的值是字符串55nav 
 ?>

四、printf函數(shù)
printf函數(shù)返回一個(gè)格式化后的字符串。
語(yǔ)法:printf(format,arg1,arg2,arg++)
參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號(hào) (“%”) 開始到轉(zhuǎn)換字符結(jié)束。下面是可能的 format 值:
* %% – 返回百分比符號(hào)
* %b – 二進(jìn)制數(shù)
* %c – 依照 ASCII 值的字符
* %d – 帶符號(hào)十進(jìn)制數(shù)
* %e – 可續(xù)計(jì)數(shù)法(比如 1.5e+3)
* %u – 無(wú)符號(hào)十進(jìn)制數(shù)
* %f – 浮點(diǎn)數(shù)(local settings aware)
* %F – 浮點(diǎn)數(shù)(not local settings aware)
* %o – 八進(jìn)制數(shù)
* %s – 字符串
* %x – 十六進(jìn)制數(shù)(小寫字母)
* %X – 十六進(jìn)制數(shù)(大寫字母)
arg1, arg2, arg++ 等參數(shù)將插入到主字符串中的百分號(hào) (%) 符號(hào)處。該函數(shù)是逐步執(zhí)行的,在第一個(gè) % 符號(hào)中,插入 arg1,在第二個(gè) % 符號(hào)處,插入 arg2,依此類推。如果 % 符號(hào)多于 arg 參數(shù),則您必須使用占位符。占位符被插入 % 符號(hào)之后,由數(shù)字和 “\$” 組成??墒褂脭?shù)字指定顯示的參數(shù),詳情請(qǐng)看例子。
例子:
復(fù)制代碼 代碼如下:

<?php
printf("My name is %s %s。","55nav", "com"); // My name is 55nav com。
printf("My name is %1\$s %1\$s","55nav", "com"); // 在s前添加1\$或2\$.....表示后面的參數(shù)顯示的位置,此行輸出 My name is 55nav 55nav因?yàn)橹伙@示第一個(gè)參數(shù)兩次。
printf("My name is %2\$s %1\$s","55nav", "com"); // My name is com 55nav 
?>

五、sprintf函數(shù)
此函數(shù)使用方法和printf一樣,唯一不同的就是該函數(shù)把格式化的字符串寫寫入一個(gè)變量中,而不是輸出來(lái)。
例子:
復(fù)制代碼 代碼如下:

<?php
sprintf("My name is %1\$s %1\$s","55nav", "com");  //你會(huì)發(fā)現(xiàn)沒有任何東西輸出的。 
$out = sprintf("My name is %1\$s %2\$s","55nav", "com"); 
echo $out;  //輸出 My name is 55nav com 
?>

六、var_dump函數(shù)
功能: 輸出變量的內(nèi)容、類型或字符串的內(nèi)容、類型、長(zhǎng)度。常用來(lái)調(diào)試。
復(fù)制代碼 代碼如下:

<?php
$a=100;
var_dump($a); //int(100)
$a=100.356;
var_dump($a); //float(100.356)
?>

相關(guān)文章

最新評(píng)論