[php] fprintf() 函數把格式化的字符串寫到指定的輸出流(例如:文件或數據庫)。

語法

fprintf(stream,format,arg1,arg2,arg++)

參數     描述
stream     可選。規定在哪裡寫/輸出字符串。
format     必需。轉換格式。
arg1     必需。規定插到 format 字符串中第一個 % 符號處的參數。
arg2     可選。規定插到 format 字符串中第二個 % 符號處的參數。
arg++     可選。規定插到 format 字符串中第三、四等等 % 符號處的參數。

參數 format 是轉換的格式,以百分比符號 ("%") 開始到轉換字符結束。下面的可能的 format 值:

    %% - 返回百分比符號
    %b - 二進制數
    %c - 依照 ASCII 值的字符
    %d - 帶符號十進制數
    %e - 可續計數法(比如 1.5e+3)
    %u - 無符號十進制數
    %f - 浮點數(local settings aware)
    %F - 浮點數(not local settings aware)
    %o - 八進制數
    %s - 字符串
    %x - 十六進制數(小寫字母)
    %X - 十六進制數(大寫字母)


範例
範例 1

<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo fprintf($file,"%s world. Day number %u",$str,$number);
?>

輸出:

27

以下文本將寫入 "test.txt":

Hello world. Day number 123

範例 2

<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"%f",$number);
?>

輸出:

123.000000

範例 3

使用佔位符:

<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f\nWith no decimals: %1\$u",$number);
?>

以下文本將寫入 "test.txt":

With 2 decimals: 123.00
With no decimals: 123



arrow
arrow
    全站熱搜
    創作者介紹
    創作者 stockwfj3 的頭像
    stockwfj3

    程式設計@筆記

    stockwfj3 發表在 痞客邦 留言(0) 人氣()