PHP vsprintf() Function
PHP vsprintf() Function
The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments.
Syntax
vsprintf ( string $format , array $args )
Parameter
format(required)- This parameter specifies the string and how to format the variables in it.
The conversion specification of this parameter follows the given prototype: %[flags][width][.precision]specifier.
Flags
Flag | Description |
- | Left-justify within the given field width and by default, it follows the right justification |
+ | It signifies the prefix positive numbers with a plus sign (+). By default, only negative is prefixed with a negative sign. |
(space) | It pads the result with spaces. This is the default. |
0 | It only left-pads numbers with zeros. With “s” specifiers this can also right-pad with zeros. |
(char) | It pads the result with the character (char). |
Width
It signifies an integer that states how many characters (minimum) this conversion should result in.
Precision
- e, E, f and F specifiers: The number of digits to be printed after the decimal point (by default, it is 6).
- g and G specifiers: The maximum number of significant digits to be printed.
- s specifier: It acts as a cutoff point, setting a maximum character limit to the string.
If the period is specified without an explicit value for precision, 0 is assumed.
Specifiers
Specifier | Description |
% | It represents a literal percent character, and no other argument is required. |
B | In this, the argument is treated as an integer and presented as a binary number. |
C | In this, the argument is treated as an integer and presented as the character with that ASCII. |
D | In this, the argument is treated as an integer and presented as a (signed) decimal number. |
E | In this, the argument is treated as scientific notation. The precision specifier stands for the number of digits after the decimal point. |
E | It is like the “e” specifier, but it uses the uppercase letter. |
F | The argument is treated as a float and presented as a floating-point number (locale aware). |
F | The argument is treated as a float and presented as a floating-point number (non-locale aware). |
G | It represents a general format. |
G | Unlike the “g” specifier but it also uses E and F. |
O | In this, the argument is treated as an integer and presented as an octal number. |
S | In this, the argument is treated and presented as a string. |
U | In this, the argument is treated as an integer and presented as an unsigned decimal number. |
X | The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). |
X | The argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). |
Type Handling
Type | Specifiers |
String | S |
Integer | d, u, c, o, x, X, b |
Double | g, G, e, E, f, F |
args(optional)- This parameter represents an array with arguments inserted at the third, fourth, etc. %-sign in the format string.
Return
This function returns the array values after formatting the specified string according to the given format or return FALSE on failure.
Example 1
Output
321.000000654.000000
Example 2
Output
%c = 2
Example 3
Output
[Hello] [Hello PHP] [***Hello] [Hello PHP] [000Hello] [Hello PH]
Example 4
Output
The unsigned decimal number for 126789 is:126789 The unsigned decimal number for -126789 is:18446744073709424827 The Floating-point number for 126789 is:126789.000000 The Floating-point number for -126789 is:-126789.000000 The Shorter value of %e and %f 126789 is:126789 The Shorter value of %E and %f -126789 is:18446744073709424827