PHP基礎 echo、print、print_rvar_dump的區別比較

來源:互聯網
上載者:User

標籤:expression   資訊   而且   

  PHP基礎教程:(基礎篇)echo、print、print_r、printf、sprintf、var_dump的區別比較

  一、echo

  echo() 實際上不是一個函數,是php語句,因此您無需對其使用括弧。不過,如果您希望向 echo() 傳遞一個以上的參數,那麼使用括弧會發生解析錯誤。而且echo是返回void的,並不傳回值,所以不能使用它來賦值。

  例子:

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00T1313.png" alt="達內培訓" style="border:0px;" />

  二、print

  print() 和 echo() 用法一樣,但是echo速度會比print快一點點。實際上它也不是一個函數,因此您無需對其使用括弧。不過,如果您希望向print() 傳遞一個以上的參數,那麼使用括弧會發生解析錯誤。注意print總是返回1的,這個和echo不一樣,也就是可以使用print來賦值,不過沒有實際意義。

  例子:

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00T95K.png" alt="達內培訓" style="border:0px;" />

  三、print_r 函數

  print_r函數列印關於變數的易於理解的資訊。

  文法:mixed print_r ( mixed $expression [, bool return ] )

  如果變數是string , integer or float , 將會直接輸出其值,如果變數是一個數組,則會輸出一個格式化後的數組,便於閱讀,也就是有key和value對應的那種格式。對於object對象類同。print_r有兩個參數,第一個是變數,第二個可設為true,如果設為true,則會返回字串,否則返回布爾值TRUE。

  例子:

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00UO95.png" alt="達內培訓" style="border:0px;" />

  四、printf函數

  printf函數返回一個格式化後的字串。

  文法:printf(format,arg1,arg2,arg++)

  參數 format 是轉換的格式,以百分比符號 (“%”) 開始到轉換字元結束。下面是可能的 format 值:

  * %% – 返回百分比符號

  * %b – 位元

  * %c – 依照 ASCII 值的字元

  * %d – 帶符號十進位數

  * %e – 可續計數法(比如 1.5e+3)

  * %u – 無符號十進位數

  * %f – 浮點數(local settings aware)

  * %F – 浮點數(not local settings aware)

  * %o – 八位元

  * %s – 字串

  * %x – 十六進位數(小寫字母)

  * %X – 十六進位數(大寫字母)

  arg1, arg2, arg++ 等參數將插入到主字串中的百分比符號 (%) 符號處。該函數是逐步執行的,在第一個 % 符號中,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。如果 % 符號多於 arg 參數,則您必須使用預留位置。預留位置被插入 % 符號之後,由數字和 “\$” 組成。可使用數字指定顯示的參數,詳情請看例子。

  例子:

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00ZNS.png" alt="達內培訓" style="border:0px;" />

  五、sprintf函數

  此函數使用方法和printf一樣,唯一不同的就是該函數把格式化的字串寫寫入一個變數中,而不是輸出來。

  例子:

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q0091K94.png" alt="達內培訓" style="border:0px;" />

  六、var_dump函數

  功能: 輸出變數的內容、類型或字串的內容、類型、長度。常用來調試。

  650) this.width=650;" src="http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00924635.png" alt="達內培訓" style="border:0px;" />


PHP基礎 echo、print、print_rvar_dump的區別比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.