Differences between sprintf and printf functions in php
This article mainly introduces the difference between sprintf and printf functions in php. For more information, see
The following is an example: Rounding to the last two decimal places
The Code is as follows:
<? Php
$ Num1 = 21;
Echo sprintf ("% 0.2f", $ num1). "<br/>"; // output 21.00
$ Num2 = 16.3287;
Echo sprintf ("% 0.2f", $ num2). "<br/>"; // output 16.33
$ Num3 = 32.12329;
Echo sprintf ("% 0.2f", $ num3). "<br/>"; // output 32.12
?>
Explain the meaning of % 0.2f:
% Indicates the start character.
0 indicates that the vacancy is filled with 0
2 indicates two digits after the decimal point
F indicates converting to a floating point number.
Conversion character
=-
% Indicates the percentage, which is not converted.
The integer B is converted into binary.
Convert an integer in c to an ASCII character.
D integer to decimal place.
F times the precision number to the floating point number.
O integer to octal.
The s integer is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
Differences between printf and sprintf
1. printf function:
Int printf (string format [, mixed args [, mixed...])
Produces output according to format, which is described in the documentation for sprintf ().
Returns the length of the outputted string.
Format the text and output it, for example:
The Code is as follows:
$ Name = "hunte ";
$ Age = 25;
Printf ("my name is % s, age % d", $ name, $ age );
2. sprintf function:
String sprintf (string format [, mixed args [, mixed...])
Returns a string produced according to the formatting string format.
Similar to printf, but not printed, the formatted text is returned. Others are the same as printf.
3. print function:
Yes. One value can be returned and only one parameter is allowed.
Int print (string arg)
Outputs arg. Returns 1, always.