PHP sprintf() Function
PHP sprintf() Function
The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function.
The related functions are as follow:
Syntax
sprintf (format, agr1, agr2, arg3...)
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, its 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 |
arg1(required)- This parameter represents the value to be inserted at the first %-sign in the format string.
arg2(optional)- This parameter represents the value to be inserted at the second %-sign in the format string.
arg++( optional)- This parameter represents the value to be inserted at the third, fourth, etc. %-sign in the format string.
Return
This function returns a string produced according to the formatting string format, or FALSE on failure.
Example 1
<?php // initializing the number $num = 134523; echo "The number: ".$num; // Returns a string according to the %f format $text = sprintf("%f",$num); // printing the sprint() function returned value echo "\nThe sprintf() function will return: ".$text; ?>
Output
The number: 134523 The sprintf() function will return: 134523.000000
Example 2
<?php // initializing a positive number $num1 = 126789; // initializing a negative number $num2 = -126789; // initializing the character value $char = 50; // The ASCII Character 50 is 2 echo"The ASCII Character for ".$char." is:". sprintf("%c",$char)."\n"; // return 2 echo "The signed decimal number for ".$num1." is:".sprintf("%d",$num1)."\n"; // will return a number 126789 echo "The signed decimal number for ".$num2." is:". sprintf("%d",$num2)."\n"; //will return a number -126789 ?>
Output
The ASCII Character for 50 is:2 The signed decimal number for 126789 is:126789 The signed decimal number for -126789 is:-126789
Example 3
<?php // initializing a positive number $num1 = 126789; // initializing a negative number $num2 = -126789; echo "The unsigned decimal number for ".$num1." is:".sprintf("%u",$num1)."\n"; echo "The unsigned decimal number for ".$num2." is:". sprintf("%u",$num2)."\n"; echo "The Floating-point number for ".$num1." is:".sprintf("%f",$num1)."\n"; echo "The Floating-point number for ".$num2." is:". sprintf("%F",$num2)."\n"; echo "The Shorter value of %e and %f ".$num1." is:".sprintf("%u",$num1)."\n"; echo "The Shorter value of %E and %f ".$num2." is:". sprintf("%u",$num2)."\n"; ?>
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
Example 4
<?php //initializing string 1 $str1 = "Hello"; // initializing string 2 $str2 = "Hello PHP"; echo sprintf("[%s]",$str1); echo "\n"; echo sprintf("[%8s]",$str2); echo "\n"; echo sprintf("[%'*8s]",$str1); echo "\n"; echo sprintf("[%-8s]",$str2); echo "\n"; echo sprintf("[%08s]",$str1); echo "\n"; echo sprintf("[%8.8s]",$str2); ?>
Output
[Hello] [Hello PHP] [***Hello] [Hello PHP] [000Hello] [Hello PH]
The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function.
The related functions are as follow:
- printf()
- vprintf()
- vsprintf()
- fprintf()
- vfprintf()
Syntax
sprintf (format, agr1, agr2, arg3...)
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, its 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 |
arg1(required)- This parameter represents the value to be inserted at the first %-sign in the format string.
arg2(optional)- This parameter represents the value to be inserted at the second %-sign in the format string.
arg++( optional)- This parameter represents the value to be inserted at the third, fourth, etc. %-sign in the format string.
Return
This function returns a string produced according to the formatting string format, or FALSE on failure.
Example 1
<?php // initializing the number $num = 134523; echo "The number: ".$num; // Returns a string according to the %f format $text = sprintf("%f",$num); // printing the sprint() function returned value echo "\nThe sprintf() function will return: ".$text; ?>
Output
The number: 134523 The sprintf() function will return: 134523.000000
Example 2
<?php // initializing a positive number $num1 = 126789; // initializing a negative number $num2 = -126789; // initializing the character value $char = 50; // The ASCII Character 50 is 2 echo"The ASCII Character for ".$char." is:". sprintf("%c",$char)."\n"; // return 2 echo "The signed decimal number for ".$num1." is:".sprintf("%d",$num1)."\n"; // will return a number 126789 echo "The signed decimal number for ".$num2." is:". sprintf("%d",$num2)."\n"; //will return a number -126789 ?>
Output
The ASCII Character for 50 is:2 The signed decimal number for 126789 is:126789 The signed decimal number for -126789 is:-126789
Example 3
<?php // initializing a positive number $num1 = 126789; // initializing a negative number $num2 = -126789; echo "The unsigned decimal number for ".$num1." is:".sprintf("%u",$num1)."\n"; echo "The unsigned decimal number for ".$num2." is:". sprintf("%u",$num2)."\n"; echo "The Floating-point number for ".$num1." is:".sprintf("%f",$num1)."\n"; echo "The Floating-point number for ".$num2." is:". sprintf("%F",$num2)."\n"; echo "The Shorter value of %e and %f ".$num1." is:".sprintf("%u",$num1)."\n"; echo "The Shorter value of %E and %f ".$num2." is:". sprintf("%u",$num2)."\n"; ?>
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
Example 4
<?php //initializing string 1 $str1 = "Hello"; // initializing string 2 $str2 = "Hello PHP"; echo sprintf("[%s]",$str1); echo "\n"; echo sprintf("[%8s]",$str2); echo "\n"; echo sprintf("[%'*8s]",$str1); echo "\n"; echo sprintf("[%-8s]",$str2); echo "\n"; echo sprintf("[%08s]",$str1); echo "\n"; echo sprintf("[%8.8s]",$str2); ?>
Output
[Hello] [Hello PHP] [***Hello] [Hello PHP] [000Hello] [Hello PH]