亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP echo,print,printf,sprintf函數(shù)之間的區(qū)別與用法詳解

 更新時間:2013年11月27日 08:48:41   作者:  
這篇文章主要是對PHP中echo,print,printf,sprintf函數(shù)之間的區(qū)別與用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助

1. echo函數(shù):

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


2. print函數(shù):

是函數(shù),可以返回一個值,只能有一個參數(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)制有符號整數(shù)
%u 十進(jìn)制無符號整數(shù)
%f 浮點(diǎn)數(shù)
%s 字符串
%c 單個字符
%p 指針的值
%e 指數(shù)形式的浮點(diǎn)數(shù)
%x, %X 無符號以十六進(jìn)制表示的整數(shù)
%o 無符號以八進(jìn)制表示的整數(shù)
%g 自動選擇合適的表示法


說明:

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

?、倮? %3d 表示輸出3位整型數(shù), 不夠3位右對齊。

?、?9.2f 表示輸出場寬為9的浮點(diǎn)數(shù), 其中小數(shù)位為2, 整數(shù)位為6, 小數(shù)點(diǎn)占一位, 不夠9位右對齊。

?、?8s 表示輸出8個字符的字符串, 不夠8個字符右對齊。

?、苋绻址拈L度、或整型數(shù)位數(shù)超過說明的場寬, 將按其實際長度輸出。

?、莞↑c(diǎn)數(shù), 若整數(shù)部分位數(shù)超過了說明的整數(shù)位寬度, 將按實際整數(shù)位輸出;

?、扌?shù)部分位數(shù)超過了說明的小數(shù)位寬度, 則按說明的寬度以四舍五入輸出。

?、呷粝朐谳敵鲋登凹右恍?, 就應(yīng)在場寬項前加個0。

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

  ⑧如果用浮點(diǎn)數(shù)表示字符或整型量的輸出格式, 小數(shù)點(diǎn)后的數(shù)字代表最大寬度, 小數(shù)點(diǎn)前的數(shù)字代表最小寬度。

   例如: %6.9s 表示顯示一個長度不小于6且不大于9的字符串。若大于9, 則第9個字符以后的內(nèi)容將被刪除。


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

   ①例如: %ld 表示輸出long整數(shù)

   ②%lf 表示輸出double浮點(diǎn)數(shù)


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

 ?、倮? %-7d 表示輸出7位整數(shù)左對齊

 ?、?-10s 表示輸出10個字符左對齊


(4). 一些特殊規(guī)定字符

    ①/n 換行
 ?、?f 清屏并換頁
 ?、?r 回車
 ?、?t Tab符
  ⑤/xhh 表示一個ASCII碼用16進(jìn)表示,
 ?、奁渲衕h是1到2個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 
?> 

相關(guān)文章

最新評論