sprintf() 函數用於輸出格式化的字符串。
用法
sprintf(format, arg1, arg2, arg++)
參數
format- 指定字符串以及如何格式化其中的變量。
以下是可能的格式值 -
%% - 返回百分號
%b - 二進製數
%c - 根據 ASCII 值的字符
%d - 有符號十進製數(負、零或正)
%e - 使用小寫的科學記數法(例如 1.2e+2)
%E - 使用大寫的科學記數法(例如 1.2E+2)
%u - 無符號十進製數(等於或大於零)
%f - 浮點數(本地設置感知)
%F - 浮點數(不知道本地設置)
%g - %e 和 %f 中的較短
%G - %E 和 %f 中的較短者
%o - 八進製數
%s - 字符串
%x - 十六進製數(小寫字母)
%X - 十六進製數(大寫字母)
argument1- 要在格式字符串中的第一個 % 符號處插入的參數。
argument2- 要在格式字符串中的第二個 % 符號處插入的參數。
返回
sprintf() 函數返回一個格式化的字符串。
示例
以下是一個例子 -
<?php
$val = 299;
$txt = sprintf("%f",$val);
echo $txt;
?>
輸出
以下是輸出 -
299.000000
示例
讓我們看另一個例子 -
<?php
$val = 768776;
$char = 95;
echo sprintf("%%b = %b",$val)."<br>";
echo sprintf("%%c = %c",$char);
?>
輸出
以下是輸出 -
%b = 10111011101100001000 %c = _
相關用法
- PHP split()用法及代碼示例
- PHP spliti()用法及代碼示例
- PHP string rtrim()用法及代碼示例
- PHP strsrt()用法及代碼示例
- PHP sqrt()用法及代碼示例
- PHP stats_dens_pmf_binomial()用法及代碼示例
- PHP string printf()用法及代碼示例
- PHP string ord()用法及代碼示例
- PHP string join()用法及代碼示例
- PHP Spreadsheet_Excel_Writer setAlign()用法及代碼示例
- PHP strtolower()用法及代碼示例
- PHP Spreadsheet_Excel_Writer setScript()用法及代碼示例
- PHP str_split()用法及代碼示例
- PHP stream_get_filters()用法及代碼示例
- PHP Spreadsheet_Excel_Writer setOutLine()用法及代碼示例
- PHP strchr()用法及代碼示例
- PHP substr_compare()用法及代碼示例
- PHP session_gc()用法及代碼示例
- PHP stats_cdf_chisquare()用法及代碼示例
- PHP string sha1()用法及代碼示例
注:本文由純淨天空篩選整理自Karthikeya Boyini大神的英文原創作品 sprintf() function in PHP。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。