[php]printf — 輸出格式化字符串

語法

printf(format,arg1,arg2,arg++)

參數     描述
format     必需。規定字符串以及如何格式化其中的變量。
arg1     必需。規定插到格式化字符串中第一個 % 符號處的參數。
arg2     可選。規定插到格式化字符串中第二個 % 符號處的參數。
arg++     可選。規定插到格式化字符串中第三、四等等 % 符號處的參數。
說明

arg1, arg2, ++ 等參數將插入到主字符串中的百分號 (%) 符號處。該函數是逐步執行的。在第一個 % 符號中,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。

例子
例子 1

<?php
$str = "Hello";
$number = 123;
printf("%s world. Day number %u",$str,$number);
?>

輸出:

Hello world. Day number 123

例子 2

<?php
$number = 123;
printf("%f",$number);
?>

輸出:

123.000000

例子 3

使用佔位符:

<?php
$number = 123;
printf("With 2 decimals: %1\$.2f<br />With no decimals: %1\$u",$number);
?>

輸出:

With 2 decimals: 123.00
With no decimals: 123


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

    程式設計@筆記

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