這篇文章主要是對(duì)PHP中echo,print,printf,sprintf函數(shù)之間的區(qū)別與用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助。
1. echo函數(shù):
輸出函數(shù),是命令,不能返回值。echo后面可以跟很多個(gè)參數(shù),之間用分號(hào)隔開,如:
- echo $myvar1;
- echo 1,2,$myvar,"<b>bold</b>";
2. print函數(shù):
是函數(shù),可以返回一個(gè)值,只能有一個(gè)參數(shù)。
int print ( string arg )
Outputs arg . Returns 1 , always.
3. printf函數(shù):
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函數(shù):
string sprintf ( string format [, mixed args [, mixed ...]] )
Returns a string produced according to the formatting string format .
跟printf相似,但不打印,而是返回格式化后的文字,其他的與printf一樣。
5. 詳細(xì)講解printf()函數(shù):
printf()函數(shù)的調(diào)用格式為:
printf("<格式化字符串>", <參量表>);
%d 十進(jìn)制有符號(hào)整數(shù)
%u 十進(jìn)制無(wú)符號(hào)整數(shù)
%f 浮點(diǎn)數(shù)
%s 字符串
%c 單個(gè)字符
%p 指針的值
%e 指數(shù)形式的浮點(diǎn)數(shù)
%x, %X 無(wú)符號(hào)以十六進(jìn)制表示的整數(shù)
%o 無(wú)符號(hào)以八進(jìn)制表示的整數(shù)
%g 自動(dòng)選擇合適的表示法
說(shuō)明:
(1). 可以在"%"和字母之間插進(jìn)數(shù)字表示最大場(chǎng)寬。
①例如: %3d 表示輸出3位整型數(shù), 不夠3位右對(duì)齊。
②%9.2f 表示輸出場(chǎng)寬為9的浮點(diǎn)數(shù), 其中小數(shù)位為2, 整數(shù)位為6, 小數(shù)點(diǎn)占一位, 不夠9位右對(duì)齊。
③%8s 表示輸出8個(gè)字符的字符串, 不夠8個(gè)字符右對(duì)齊。
④如果字符串的長(zhǎng)度、或整型數(shù)位數(shù)超過(guò)說(shuō)明的場(chǎng)寬, 將按其實(shí)際長(zhǎng)度輸出。
⑤浮點(diǎn)數(shù), 若整數(shù)部分位數(shù)超過(guò)了說(shuō)明的整數(shù)位寬度, 將按實(shí)際整數(shù)位輸出;
⑥小數(shù)部分位數(shù)超過(guò)了說(shuō)明的小數(shù)位寬度, 則按說(shuō)明的寬度以四舍五入輸出。
⑦若想在輸出值前加一些0, 就應(yīng)在場(chǎng)寬項(xiàng)前加個(gè)0。
例如: %04d 表示在輸出一個(gè)小于4位的數(shù)值時(shí), 將在前面補(bǔ)0使其總寬度為4位。
⑧如果用浮點(diǎn)數(shù)表示字符或整型量的輸出格式, 小數(shù)點(diǎn)后的數(shù)字代表最大寬度, 小數(shù)點(diǎn)前的數(shù)字代表最小寬度。
例如: %6.9s 表示顯示一個(gè)長(zhǎng)度不小于6且不大于9的字符串。若大于9, 則第9個(gè)字符以后的內(nèi)容將被刪除。
(2). 可以在"%"和字母之間加小寫字母l, 表示輸出的是長(zhǎng)型數(shù)。
①例如: %ld 表示輸出long整數(shù)
②%lf 表示輸出double浮點(diǎn)數(shù)
(3). 可以控制輸出左對(duì)齊或右對(duì)齊, 即在"%"和字母之間加入一個(gè)"-" 號(hào)可說(shuō)明輸出為左對(duì)齊, 否則為右對(duì)齊。
①例如: %-7d 表示輸出7位整數(shù)左對(duì)齊
②%-10s 表示輸出10個(gè)字符左對(duì)齊
(4). 一些特殊規(guī)定字符
①/n 換行
②/f 清屏并換頁(yè)
③/r 回車
④/t Tab符
⑤/xhh 表示一個(gè)ASCII碼用16進(jìn)表示,
⑥其中hh是1到2個(gè)16進(jìn)制數(shù)
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
- ?>
新聞熱點(diǎn)
疑難解答