這篇文章主要是對PHP中echo,print,printf,sprintf函數之間的區別與用法進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助。
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
- $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
- ?>
新聞熱點
疑難解答