PHP sprintf() function use case analysis_PHP tutorial

WBOY
Release: 2016-07-21 15:30:02
Original
984 people have browsed it

Copy code The code is as follows:

//sprintf() function, the return value is the formatted string
string sprintf ( string $format [, mixed $args [, mixed $... ]] )

$y = 11;
$m = 3;
$d = 9;
$date = sprinf('%04d-%02d-%02d', $y, $m ,$d);
echo $date; //0011-0
//printf () function, the return value is the formatted string length
int printf ( string $format [, mixed $args [, mixed $... ]] )
$num = 3.14;
printf ("Character padding %'#6.2s", $num); //##3.14
//The character length is 6, with 2 after the dot, less than 6 digits, #Padding

The difference between sprintf() and printf()
The syntax and format are the same, but the return value is different


Definition and usage
The sprintf() function writes the formatted string into a variable .

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

Description

The parameter format is the format of the conversion, starting with the percent sign ("%") and ending with the conversion character. Possible format values ​​below:

  • %% - Returns the percent symbol
  • %b - binary number
  • %c - character according to ASCII value
  • %d - signed decimal number
  • %e - Continuous counting method (e.g. 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 (lowercase letters)
  • %X - hexadecimal number (uppercase letters)

Arguments such as arg1, arg2, ++ etc. will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.
Example
Example 1

Copy code The code is as follows:

$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:

With 2 decimals: 123.00
With no decimals: 123
For more details, please refer to http://www.jb51.net/w3school/php/func_string_sprintf.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323317.htmlTechArticleCopy the code as follows: ?php //sprintf() function, the return value is a formatted string string sprintf ( string $format [, mixed $args [, mixed $... ]] ) $y = 11; $m = 3; $d = 9;...
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