PHP echo, print, printf, sprintf函數的區別和使用

來源:互聯網
上載者:User

標籤:des   blog   http   os   使用   io   strong   for   ar   

1. echo函數:

輸出函數,是命令,不能傳回值。echo後面可以跟很多個參數,之間用分號隔開,如: 
echo $myvar1; 
echo 1,2,$myvar,"<b>bold</b>";

 

2. print函數:

是函數,可以返回一個值,只能有一個參數。

int print ( string arg )

Outputs arg . Returns 1 , always.

 

3. printf函數:

int printf ( string format [, mixed args [, mixed ...]] )

Produces output according to format , which is described in the documentation for sprintf() .

Returns the length of the outputted string.

 

把文字格式化以後輸出,如: 
$name="hunte"; 
$age=25; 
printf("my name is %s, age %d", $name, $age);

 

4. sprintf函數:

string sprintf ( string format [, mixed args [, mixed ...]] )

Returns a string produced according to the formatting string format .

 

跟printf相似,但不列印,而是返回格式化後的文字,其他的與printf一樣。

 

 

5. 詳細講解printf()函數:

 

printf()函數的調用格式為: 
printf("<格式化字串>", <參量表>);

 

%d 十進位有符號整數 
%u 十進位不帶正負號的整數 
%f 浮點數 
%s 字串 
%c 單個字元 
%p 指標的值 
%e 指數形式的浮點數 
%x, %X 無符號以十六進位表示的整數 
%o 無符號以八進位表示的整數 
%g 自動選擇合適的標記法

 

說明:

(1). 可以在"%"和字母之間插進數字表示最大場寬。

 ①例如: %3d 表示輸出3位整型數, 不夠3位靠右對齊。

 ②%9.2f 表示輸出場寬為9的浮點數, 其中小數位為2, 整數位為6, 小數點佔一位, 不夠9位靠右對齊。

 ③%8s 表示輸出8個字元的字串, 不夠8個字元靠右對齊。

 ④如果字串的長度、或整型數位元超過說明的場寬, 將按其實際長度輸出。

 ⑤浮點數, 若整數部分位元超過了說明的整數位寬度, 將按實際整數位輸出;

 ⑥小數部分位元超過了說明的小數位寬度, 則按說明的寬度以四捨五入輸出。

 ⑦若想在輸出值前加一些0, 就應在場寬項前加個0。

   例如: %04d 表示在輸出一個小於4位的數值時, 將在前面補0使其總寬度為4位。

  ⑧如果用浮點數表示字元或整型量的輸出格式, 小數點後的數字代表最大寬度, 小數點前的數字代表最小寬度。

   例如: %6.9s 表示顯示一個長度不小於6且不大於9的字串。若大於9, 則第9個字元以後的內容將被刪除。

 

(2). 可以在"%"和字母之間加小寫字母l, 表示輸出的是長型數。

   ①例如: %ld 表示輸出long整數

   ②%lf 表示輸出double浮點數

 

(3). 可以控制輸出靠左對齊或靠右對齊, 即在"%"和字母之間加入一個"-" 號可說明輸出為靠左對齊, 否則為靠右對齊。 

  ①例如: %-7d 表示輸出7位整數靠左對齊 

  ②%-10s 表示輸出10個字元靠左對齊

 

(4). 一些特殊規定字元

    ①\n 換行 
  ②\f 清屏並換頁 
  ③\r 斷行符號 
  ④\t Tab符 
  ⑤\xhh 表示一個ASCII碼用16進表示, 
  ⑥其中hh是1到2個16進位數

6. printf() : examples

例1: various examples

 

<?php<br>$n =  43951789;  $u = -43951789;  $c = 65; // ASCII 65 is ‘A‘   // notice the double %%, this prints a literal ‘%‘ character  printf("%%b = ‘%b‘\n", $n); // binary representation  printf("%%c = ‘%c‘\n", $c); // print the ascii character, same as chr() function  printf("%%d = ‘%d‘\n", $n); // standard integer representation  printf("%%e = ‘%e‘\n", $n); // scientific notation  printf("%%u = ‘%u‘\n", $n); // unsigned integer representation of a positive integer  printf("%%u = ‘%u‘\n", $u); // unsigned integer representation of a negative integer  printf("%%f = ‘%f‘\n", $n); // floating point representation  printf("%%o = ‘%o‘\n", $n); // octal representation  printf("%%s = ‘%s‘\n", $n); // string representation  printf("%%x = ‘%x‘\n", $n); // hexadecimal representation (lower-case)  printf("%%X = ‘%X‘\n", $n); // hexadecimal representation (upper-case)   printf("%%+d = ‘%+d‘\n", $n); // sign specifier on a positive integer  printf("%%+d = ‘%+d‘\n", $u); // sign specifier on a negative integer  ?>    The printout of this program would be:   %b = ‘10100111101010011010101101‘ %c = ‘A‘ %d = ‘43951789‘ %e = ‘4.39518e+7‘ %u = ‘43951789‘ %u = ‘4251015507‘ %f = ‘43951789.000000‘ %o = ‘247523255‘ %s = ‘43951789‘ %x = ‘29ea6ad‘ %X = ‘29EA6AD‘ %+d = ‘+43951789‘ %+d = ‘-43951789‘ 

 

例2: string specifiers

 

<?php$s = ‘monkey‘;  $t = ‘many monkeys‘;      printf("[%s]\n",      $s); // standard string output  printf("[%10s]\n",    $s); // right-justification with spaces  printf("[%-10s]\n",   $s); // left-justification with spaces  printf("[%010s]\n",   $s); // zero-padding works on strings too  printf("[%‘#10s]\n",  $s); // use the custom padding character ‘#‘  printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters  ?>         The printout of this program would be:   [monkey]  [    monkey]  [monkey    ]  [0000monkey]  [####monkey]  [many monke] 

 

例3:zero-padded integers

 

<?php  $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);  ?>  

 

例4:formatting currency

 

<?php  $money1 = 68.75;  $money2 = 54.35;  $money = $money1 + $money2;  // echo $money will output "123.1";  $formatted = sprintf("%01.2f", $money);  // echo $formatted will output "123.10"  ?>  

 

例5: sprintf() : scientific notation

<?php  $number = 362525200;  echo sprintf("%.3e", $number); // outputs 3.63e+8  ?>  

 

PHP echo, print, printf, sprintf函數的區別和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.