|
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)制無符號(hào)整數(shù)
%f 浮點(diǎn)數(shù)
%s 字符串
%c 單個(gè)字符
%p 指針的值
%e 指數(shù)形式的浮點(diǎn)數(shù)
%x, %X 無符號(hào)以十六進(jìn)制表示的整數(shù)
%o 無符號(hào)以八進(jìn)制表示的整數(shù)
%g 自動(dòng)選擇合適的表示法
說明:
(1). 可以在"%"和字母之間插進(jìn)數(shù)字表示最大場寬。
①例如: %3d 表示輸出3位整型數(shù), 不夠3位右對(duì)齊。
②%9.2f 表示輸出場寬為9的浮點(diǎn)數(shù), 其中小數(shù)位為2, 整數(shù)位為6, 小數(shù)點(diǎn)占一位, 不夠9位右對(duì)齊。
③%8s 表示輸出8個(gè)字符的字符串, 不夠8個(gè)字符右對(duì)齊。
④如果字符串的長度、或整型數(shù)位數(shù)超過說明的場寬, 將按其實(shí)際長度輸出。
⑤浮點(diǎn)數(shù), 若整數(shù)部分位數(shù)超過了說明的整數(shù)位寬度, 將按實(shí)際整數(shù)位輸出;
⑥小數(shù)部分位數(shù)超過了說明的小數(shù)位寬度, 則按說明的寬度以四舍五入輸出。
⑦若想在輸出值前加一些0, 就應(yī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è)長度不小于6且不大于9的字符串。若大于9, 則第9個(gè)字符以后的內(nèi)容將被刪除。
(2). 可以在"%"和字母之間加小寫字母l, 表示輸出的是長型數(shù)。
①例如: %ld 表示輸出long整數(shù)
②%lf 表示輸出double浮點(diǎn)數(shù)
(3). 可以控制輸出左對(duì)齊或右對(duì)齊, 即在"%"和字母之間加入一個(gè)"-" 號(hào)可說明輸出為左對(duì)齊, 否則為右對(duì)齊。
①例如: %-7d 表示輸出7位整數(shù)左對(duì)齊
②%-10s 表示輸出10個(gè)字符左對(duì)齊
(4). 一些特殊規(guī)定字符
①/n 換行
②/f 清屏并換頁
③/r 回車
④/t Tab符
⑤/xhh 表示一個(gè)ASCII碼用16進(jìn)表示,
⑥其中hh是1到2個(gè)16進(jìn)制數(shù)
6. printf() : examples
例1: various examples
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
例4:formatting currency
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?php
$number = 362525200;
echo sprintf("%.3e", $number); // outputs 3.63e+8
?>
php技術(shù):PHP echo,print,printf,sprintf函數(shù)之間的區(qū)別與用法詳解,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。