sprintf in php is a function used to write a formatted string to a variable and returns a formatted string. In PHP, version 4 and above support this sprintf function. sprintf() function is similar to printf() function, but the main and only difference between both of them is that the sprintf() saves the output into a string instead of displaying formatted output on browser like printf() function. sprintf() function can work with echo, i.e., the formatted string returned by sprintf() is printed on the browser using echo. Let us dig deeper into the topic and see its syntax, formats accessible, and solve few programs.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Here is the syntax of sprintf() function in PHP.
sprintf(format, arg1, arg2, arg3, …….)
Here, arg1, arg2, arg3, etc., are parameters of sprintf(). arg1 is a required argument to be inserted at the first. arg2, arg3, ……… are optional arguments to be inserted.
Format: It is the required parameter and specifies the string on how to format variables in it.
Below are the possible formats:
Parameter
|
Description |
||||||||||||||||||||||||||||||||
%b | Argument present as a binary number | ||||||||||||||||||||||||||||||||
%% | Returns % sign | ||||||||||||||||||||||||||||||||
%d | Parameter treated as a positive integer, represented as a decimal number | ||||||||||||||||||||||||||||||||
%c | Parameter treated as an integer, represented as a character with ASCII | ||||||||||||||||||||||||||||||||
%e | Precision specifier that specifies the number of digits after the decimal point. Scientific notation with lowercase | ||||||||||||||||||||||||||||||||
%u | Parameter treated as an integer, represented as unsigned integer | ||||||||||||||||||||||||||||||||
%f | Floating-point number(locale) | ||||||||||||||||||||||||||||||||
%g | General format | ||||||||||||||||||||||||||||||||
%o | Represented as Octal number | ||||||||||||||||||||||||||||||||
%x | Represented as Hexadecimal number with lowercase letters | ||||||||||||||||||||||||||||||||
%s | Argument presented and treated as a string | ||||||||||||||||||||||||||||||||
%E | Similar to %e specifier but with Uppercase. | ||||||||||||||||||||||||||||||||
%F | Floating-point number(Non-locale) | ||||||||||||||||||||||||||||||||
%G | Similar to %g specifier but uses %E and %F | ||||||||||||||||||||||||||||||||
%X | Represented with Hexadecimal number but with uppercase |
There are some additional format values, which are placed between % and letter.
Let us see How sprintf() function in PHP works with few examples,
Code:
<!DOCTYPE html> <html> <body> <?php $num1 = 321234; $num2 = 860; $text = sprintf("%f,%f",$num1, $num2); echo $text; ?> </body> </html>
Output:
Here, we have taken two float values and using sprintf() function, scanned the variables, and using echo, have printed the floating values on the console.
Code:
<!DOCTYPE html> <html> <body> <?php $num1 = 4563; $text = sprintf("With 3 decimals: %1\$.3f <br>With no decimals: %1\$u <br>With single decimal: %1\$.1f",$num1); echo $text; ?> </body> </html>
Output:
So here for floating values, we have specified as to no decimals, or single decimal, or 3 decimal values.
Code:
<!DOCTYPE html> <html> <body> <?php $string1 = 'PHPv4'; echo sprintf("[%s]",$string1)."<br>"; echo sprintf("[%08s]",$string1)."<br>"; echo sprintf("[%-8s]",$string1)."<br>"; echo sprintf("[%8s]",$string1)."<br>"; echo sprintf("[%8.8s]",$string1)."<br>"; echo sprintf("[%'*8s]",$string1)."<br>"; ?> </body> </html>
Output:
So based on the output, [%s] will return the string as it is
[%08s] will return string with zero padding [%-8s] will return string with left justification [%8s] will return string with the right justification [%8.8s] will return string with left justification, cuts of characters after a specific value [%’*8s] will return string with additional *Code:
<!DOCTYPE html> <html> <body> <?php $num = 7; $fruits = 'Mangoes'; $arg1 = 'The %2$s are %1$d in number'; echo sprintf($arg1, $num, $fruits); ?> </body> </html>
Output:
So here, format string supports argument swapping/ numbering.
Imagine if the placeholders in format string do not match the order of the arguments as shown above. And hence, we have indicated the arg1 which arguments refer to which placeholders.
Code:
<!DOCTYPE html> <html> <body> <?php $arg1 = 456; $arg2 = -456; $str = 57; echo sprintf("%%b = %b",$arg1)."<br>"; echo sprintf("%%d = %d",$arg1)."<br>"; echo sprintf("%%d = %d",$arg2)."<br>"; echo sprintf("%%c = %c",$str)."<br>"; echo sprintf("%%e = %e",$arg1)."<br>"; echo sprintf("%%u = %u",$arg1)."<br>"; echo sprintf("%%u = %u",$arg2)."<br>"; echo sprintf("%%f = %f",$arg1)."<br>"; echo sprintf("%%f = %f",$arg2)."<br>"; echo sprintf("%%g = %g",$arg1)."<br>"; echo sprintf("%%g = %g",$arg2)."<br>"; echo sprintf("%%o = %o",$arg1)."<br>"; echo sprintf("%%o = %o",$arg2)."<br>"; echo sprintf("%%x = %x",$arg1)."<br>"; echo sprintf("%%x = %x",$arg2)."<br>"; echo sprintf("%%s = %s",$arg1)."<br>"; echo sprintf("%%s = %s",$arg2)."<br>"; echo sprintf("%%E = %E",$arg1)."<br>"; echo sprintf("%%F = %F",$arg1)."<br>"; echo sprintf("%%G = %G",$arg1)."<br>"; echo sprintf("%%X = %X",$arg1)."<br>"; echo sprintf("%%+d = %+d",$arg1)."<br>"; echo sprintf("%%+d = %+d",$arg2)."<br>"; ?> </body> </html>
Output:
So here we have shown all the format specifiers.
With this, we shall conclude the topic ‘sprintf in php’. We have seen the syntax of sprintf() function in PHP. We have seen what each format specifier means and have Illustrated few examples on how to use sprintf in PHP. The above examples will give a clear understanding of all the format specifiers.
The above is the detailed content of sprintf in PHP. For more information, please follow other related articles on the PHP Chinese website!