Home > Backend Development > PHP Tutorial > Detailed explanation of the use of printf() function formatting in PHP language_PHP tutorial

Detailed explanation of the use of printf() function formatting in PHP language_PHP tutorial

WBOY
Release: 2016-07-21 14:56:35
Original
786 people have browsed it

The advantage of the printf() function is that it can format the output!
The format is as follows: %['padding_character][-][width][.precision]type
All conversion instructions start with %, if If you want to print a % symbol, you must use %%.
The parameter 'padding_character is optional. It will be used to fill the variable up to the specified width. The function of this parameter is to pad in front of the variable. The default padding character is a Space, if you specify 0 or space, you do not need ' single quote as a prefix, other characters must specify ' as a prefix.
Parameter - is optional. It refers to left alignment, and the default is right alignment.
Parameter width It refers to the length of the variable being replaced.
The parameter precision means starting from the decimal point. It specifies the number of digits to be displayed after the decimal point.
The parameter type is the type code, please see the following table:
Type | Meaning
b | | Interpreted as an integer and output as a binary.
c | Interpreted as an integer and output as a character representation (ASCII code).
d | Interpreted as an integer and output as an integer.
f | Interpreted as Double precision and output as a float.
o Interpretation as an integer and output as an octal number.
s Interpretation as a string and output as a string.
u Interpretation as an integer and output as an unspecified decimal Output.
x | Interpreted as an integer and output as a hexadecimal number with lowercase letters a-f
X | Interpreted as an integer and output as a hexadecimal number with uppercase letters A-F

Demo:
--------------------------------------------- -------------------------------------------------- -
$str = "0758 jian";
$strA = "A";
$strB = "B";
$num1 = 5;
$num2 = 5;
$num3 = 0.25;
$num4 = 3.2567;
$num5 = 8;
$num6 = 1.735;
$num7 = 16777215;
$num8 = 16777215;
printf("%2$s %1$s", $strA, $strB); // 2$ is the specified parameter position
echo '
';
printf("Padding: %'%10s", $str); //Specify the fill character as % and the string width is 10
echo '
';
printf("Binary system: %b", $num1);
echo '
';
printf("ASCII code: %c", $num2);
echo '
' ;
printf("Integer: %d", $num3);
echo '
';
printf("Floating point: %.2f", $num4);
echo '
';
printf("Octal: %o", $num5);
echo '
';
printf("String: % s", $str);
echo '
';
printf("non-decimal: %u", $num6);
echo '
';
printf("Hex: %x", $num7);
echo '
';
printf("Hex: %X", $num8) ;
?>
---------------------------------------- -------------------------------------------------- ---
Output result:
----------------------------------------- -------------------------------------------------- -----
B A
Padding: %0758 jian
Binary: 101
ASCII code:
Integer: 0
Floating point: 3.26
Octal: 10
String: 0758 jian
Non-decimal: 1
Hex: ffffff
Hex: FFFFFF

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364176.htmlTechArticleThe advantage of the printf() function is that it can format the output! The format is as follows: %['padding_character][-][width ][.precision]type All conversion instructions start with %. If you want to print a % symbol, you must...
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