The following small series will bring you an in-depth understanding of phpprintf () output formatted strings. I think it is quite good. now I will share it with you and give you a reference. Let's take a look at the php printf () function for outputting formatted strings. This article introduces the usage and basic use examples of the php printf () function to the coders, for more information, see.
Definition and usage
The printf () function outputs formatted strings.
The arg1, arg2, and arg ++ parameters are inserted to the percent sign (%) in the main string. This function is executed step by step. Insert arg1 at the first % symbol, insert arg2 at the second % symbol, and so on.
Note: If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted into the % symbol, it consists of numbers and "\ $. See Example 2.
Tip: Related functions: sprintf (), vprintf (), vsprintf (), fprintf (), and vfprintf ()
• Fprintf ()
• Sprintf ()
• Vfprintf ()
• Vprintf ()
• Vsprintf ()
Syntax
printf(format,arg1,arg2,arg++)
Instance
Example 1
Use format value % f:
Example 2
Use placeholders:
No decimals: % 1 \ $ u ", $ number);?>
Example 3
Demonstration of all possible format values:
", $ Num1); // binary number printf (" % c = % c
", $ Char); // ASCII character printf (" % d = % d
", $ Num1); // signed decimal number printf (" % d = % d
", $ Num2); // signed decimal number printf (" % e = % e
", $ Num1); // Scientific notation (lower case) printf (" % E = % E
", $ Num1); // Scientific notation (uppercase) printf (" % u = % u
", $ Num1); // unsigned decimal number (positive) printf (" % u = % u
", $ Num2); // unsigned decimal number (negative) printf (" % f = % f
", $ Num1); // floating point number (depending on local settings) printf (" % F = % F
", $ Num1); // floating point number (not depending on local settings) printf (" % g = % g
", $ Num1); // shorter than % e and % fprintf (" % G = % G
", $ Num1); // less than % E and % fprintf (" % o = % o
", $ Num1); // eight digits printf (" % s = % s
", $ Num1); // string printf (" % x = % x
", $ Num1); // hexadecimal number (lower case) printf (" % X = % X
", $ Num1); // hexadecimal number (uppercase) printf (" % + d = % + d
", $ Num1); // symbol specifier (positive) printf (" % + d = % + d
", $ Num2); // symbol specifier (negative)?>
Example 4
Demonstration of string specifiers:
",$str1);printf("[%8s]
",$str1);printf("[%-8s]
",$str1);printf("[%08s]
",$str1);printf("[%'*8s]
",$str1);printf("[%8.8s]
",$str2);?>
The above in-depth understanding of php printf () output formatted strings is all the content shared by the editor. I hope to give you a reference and support for PHP.
For more details about phpprintf () output formatted strings, please follow the PHP Chinese network!