![]() |
VOOZH | about |
The sprintf() function is an inbuilt function in PHP that is used for formatting strings in a manner similar to the C language's printf() function. It allows you to create formatted strings by substituting placeholders with corresponding values.
Syntax:
string sprintf(string $format, mixed ...$values)Parameters: This function accepts two parameters that are described below:
Return Values: The sprintf() function returns the string which is produced by this function.
Program 1: The following program demonstrates the sprintf() function.
Name: Ram Kumar Age: 28 Balance: $1200.50
Program 2: The following program demonstrates the sprintf() function.
Invoice: Product: Widget Quantity: 10 Price per Unit: $24.95 Total Price: $249.50
Program 3: In this example we iterates through an array of products, formatting and printing them as a table with aligned columns using sprintf() for string formatting.
Output
| Product | Price | Quantity |
---------------------------------------
| Laptop | $999.99 | 2 |
| Smartphone | $599.99 | 3 |
| Headphones | $99.99 | 5 |
---------------------------------------
Reference: https://www.php.net/manual/en/function.sprintf.php