php輸出語句echo、print、print_r、printf、sprintf、var_dump比較

來源:互聯網
上載者:User

標籤:

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

<?php $a = echo("xshell"); // 錯誤!不能用來賦值 echo "xshell"; // xshell  echo ("xshell"); // xshell  echo ("xshell","net"); //發生錯誤,有括弧不能傳遞多個參數  echo "xshell"," net"," is", " web";  // 不用括弧的時候可以用逗號隔開多個值, 會輸出 xshell net is web  echo "xshell is  good  web.";  // 不管是否換行,最終顯示都是為一行 xshell is good web.  echo "$fistname net"; // 如果 $firstname = "xshell", 則會輸出 xshell net.  echo ‘$firstname net‘; // 由於使用單引號,所以不會輸出$firstname的值,而是輸出 $firstname net ?>

 

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

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

 

三、print_r 函數
    print_r函數列印關於變數的易於理解的資訊。
    文法:mixed print_r ( mixed $expression [, bool return ] )
    如果變數是string , integer or float , 將會直接輸出其值,如果變數是一個數組,則會輸出一個格式化後的數組,便於閱讀,也就是有key和value對應的那種格式。對於object對象類同。print_r有兩個參數,第一個是變數,第二個可設為true,如果設為true,則會返回字串,否則返回布爾值TRUE。
例子:

<?php $a="xshell"; $c = print_r($a);  echo $c;  // $c的值是TRUE  $c = print_r($a, ture);  echo $c; // $c的值是字串xshell?>

 

四、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 參數,則您必須使用預留位置。預留位置被插入 % 符號之後,由數字和 “\$” 組成。可使用數字指定顯示的參數,詳情請看例子。
例子:

<?php printf("My name is %s %s。","xshell", "net"); // My name is xshell net。 printf("My name is %1\$s %1\$s","xshell", "net"); // 在s前添加1\$或2\$.....表示後面的參數顯示的位置,此行輸出 My name is Ricky Ricky因為只顯示第一個參數兩次。 printf("My name is %2\$s %1\$s","xshell", "net"); // My name is net xshell ?>

 

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

<?php sprintf("My name is %1\$s %1\$s","xshell", "net");  //你會發現沒有任何東西輸出的。  $out = sprintf("My name is %1\$s %2\$s","xshell", "net");  echo $out;  //輸出 My name is xshell net ?>

 

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

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

php輸出語句echo、print、print_r、printf、sprintf、var_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.