Home > Backend Development > PHP Tutorial > PHP sprintf function use case analysis

PHP sprintf function use case analysis

WBOY
Release: 2016-07-29 08:45:09
Original
1280 people have browsed it

Copy the 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++)
ParametersDescription
formatRequired. Convert format.
arg1Required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2Optional. Specifies the parameter to be inserted into the format string at the second % sign.
arg++Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.

Description

Parameter format is the converted format, 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 (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 (lowercase letters)
  • %X - Hexadecimal number (capital letters)

arg1, arg2, ++, etc. are 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, 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 the code The code is as follows:


< ;?php
$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

The above has introduced the use case analysis of the PHP sprintf function, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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