The PHP printf () function is used to output formatted strings, this article introduces the PHP printf () function of the use of the method and the basic use of examples, interested in the code farmers can refer to.
Definitions and usage
The printf () function outputs a formatted string.
The Arg1, arg2, arg++ parameters are inserted into the percent sign (%) symbol in the main string. The function is executed incrementally. At the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.
Note: If the% symbol is greater than the ARG argument, you must use a placeholder. Placeholders are inserted after the% symbol, consisting of numbers and "\$." See Example 2.
Tip: Related functions: sprintf (), vprintf (), vsprintf (), fprintf (), and vfprintf ()
fprintf ()
sprintf ()
vfprintf ()
vprintf ()
vsprintf ()
Grammar
printf (format,arg1,arg2,arg++)
| Parameters |
Description |
| Format |
Necessary. Specify the string and how to format the variables therein.
Possible format values:
- Percent%-returns a percent percent
- %b-Binary number
- The character corresponding to the%C-ASCII value
- %d-contains the positive and negative decimal digits (negative, 0, positive numbers)
- %e-Scientific notation using lowercase (for example, 1.2e+2)
- %E-scientific notation using uppercase (for example, 1.2E+2)
- %u-a decimal number (greater than or equal to 0) that does not contain a positive sign
- %f-floating-point numbers (Local settings)
- %F-floating-point number (not local setting)
- %g-Shorter%e and%f
- %G-Shorter%E and%f
- %o-Eight binary number
- %s-String
- %x-16 Number (small letter)
- %x-16 Number (capital letter)
The attached format value. Must be placed between% and letters (e.g.%.2f):
- + (precede the number with + or-to define the positive and negative of the number.) By default, only negative numbers are marked and positive numbers are not marked.
- ' (Specify what to use as padding, default is a space.) It must be used in conjunction with the width designation. )
- -(left to adjust the value of the variable)
- [0-9] (Specify the minimum width of the variable value)
- . [0-9] (Specify number of decimal digits or maximum string length)
Note: If you use more than one of the above format values, they must be used in the order listed above and cannot be disturbed. |
| Arg1 |
Necessary. Specify the parameter to be inserted into the first% symbol in the format string. |
| Arg2 |
Necessary. Specify the parameter to be inserted into the second% symbol in the format string. |
| arg++ |
Optional. Specify the parameters to be inserted into the format string at the third to fourth and so% symbols. |
Technical details
| return value: |
Returns the length of the string being output. |
| PHP version: |
4 + |
Instance
Example 1
Use format value%f:
<?php
$number = 123;
printf ("%f", $number);
? >
Example 2
Use placeholders:
<?php
$number = 123;
printf ("There are two decimal:%1\$.2f<br> no decimal places:%1\ $u", $number);
? >
Example 3
Demo of all possible format values:
<?php $num 1 = 123456789;
$num 2 =-123456789; $char = 50; ASCII character 50 is 2//NOTE: The format value "%" returns a percent of printf ("%%b =%b <br>", $num 1); Binary number printf ("%%c =%c <br>", $char); ASCII character printf ("%%d =%d <br>", $num 1); Signed decimal number printf ("%%d =%d <br>", $num 2); Signed decimal number printf ("%%e =%e <br>", $num 1); Scientific notation (lowercase) printf ("%%e =%E <br>", $num 1); Scientific notation (uppercase) printf ("%%u =%u <br>", $num 1); unsigned decimal number (positive) printf ("%%u =%u <br>", $num 2); unsigned decimal (negative) printf ("%%f =%f <br>", $num 1); Floating-point number (depending on local settings) printf ("%%f =%F <br>", $num 1); Floating-point number (not local setting) printf ("%%g =%g <br>", $num 1); Shorter than%e and%f printf ("%%g =%G <br>", $num 1); Shorter than%E and%f printf ("%%o =%o <br>", $num 1); Octal number printf ("%%s =%s <br>", $num 1); String printf ("%%x =%x <br>", $num 1); Hexadecimal number (lowercase) printf ("%%x =%x <br>", $num 1); Hexadecimal number (uppercase) printf ("%%+d =%+d <br>", $num 1); Symbol specifier (positive) printf ("%%+d =%+d <br>", $NUM2); Symbol specifier (negative)?>
Example 4
A demo of the string descriptor:
<?php
$str 1 = "Hello";
$str 2 = "Hello world!";
printf ("[%s]<br>", $str 1);
printf ("[%8s]<br>", $str 1);
printf ("[%-8s]<br>", $str 1);
printf ("[%08s]<br>", $str 1);
printf ("[% ' *8s]<br>", $str 1);
printf ("[%8.8s]<br>", $str 2);
? >
The above in-depth understanding of the PHP printf () format of the string is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.