Home > Backend Development > PHP Tutorial > Application of PHP sprintf() function (definition and usage)_PHP tutorial

Application of PHP sprintf() function (definition and usage)_PHP tutorial

WBOY
Release: 2016-07-21 15:18:33
Original
819 people have browsed it

Grammar

sprintf(format,arg1,arg2,arg++)
Copy after login
参数 描述
format 必需。转换格式。
arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。
arg2 可选。规定插到 format 字符串中第二个 % 符号处的参数。
arg++ 可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。

Description

The parameter format is the conversion format, starting with the percent sign ("%") and ending with the conversion character. Possible format values ​​below:
%% - Returns percent symbol
%b - Binary number
%c - Character according to ASCII value
%d - Signed decimal Number
%e - continuous notation (such as 1.5e+3)
%u - unsigned decimal number
%f - floating point number (local settings aware)
%F - floating point number ( not local settings aware)
%o - octal number
%s - string
%x - hexadecimal number (lower case letters)
%X - hexadecimal number (upper case letters) )
arg1, arg2, ++ and other parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, insert arg2, and so on

Tips and Notes
Note: If there are more % signs than arg arguments, you must Use placeholders. The placeholder is inserted after the % symbol and consists of a number and "$". See example 3.

Example
Example 1

Copy code The code is as follows:

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

Output:

Hello world. Day number 123

Example 2
Copy code The code is as follows:

$number = 123;
$txt = sprintf("%f",$number) ;
echo $txt;
?>


Output:

123.000000

Example 3
Copy code The code is as follows:

$number = 123;
$txt = sprintf("With 2 decimals: %1$.2f
With no decimals: %1$u",$number);
echo $txt;
?>

Output:
Copy code The code is as follows:

With 2 decimals: 123.00
With no decimals: 123

Example 4
Copy code The code is as follows:

$ctype_primary = strtolower('application ');
$ctype_secondary = strtolower('pdf');
$mimetype = sprintf('%s/%s', $ctype_primary, $ctype_secondary);
echo $mimetype;
? >

Output:
Copy code The code is as follows:

application/pdf

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325510.htmlTechArticleSyntax sprintf(format,arg1,arg2,arg++) Parameter Description format required. Convert format. arg1 required. Specifies the parameters to be inserted at the first % sign in the format string. arg2 optional. Regulations...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template