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:
- $format: This is a string that specifies the format of the output. It contains placeholders that begin with a percent sign (%) followed by a character representing the type of value that should be inserted at that position.
- $values: These are the values that will replace the placeholders in the format string. You can provide any number of arguments, depending on the number of placeholders in the format string and their types.
Return Values: The sprintf() function returns the string which is produced by this function.
Program 1: The following program demonstrates the sprintf() function.
PHP
<?php
$first_name = "Ram";
$last_name = "Kumar";
$age = 28;
$balance = 1200.50;
$formatted_string = sprintf(
"Name: %s %s\nAge: %d\nBalance: $%.2f",
$first_name, $last_name, $age, $balance
);
echo $formatted_string;
?>
OutputName: Ram Kumar
Age: 28
Balance: $1200.50
Program 2: The following program demonstrates the sprintf() function.
PHP
<?php
$product = "Widget";
$quantity = 10;
$pricePerUnit = 24.95;
$totalPrice = $quantity * $pricePerUnit;
$formatted_invoice = sprintf(
"Invoice:\nProduct: %s\nQuantity: %d\nPrice"
. " per Unit: $%.2f\nTotal Price: $%.2f",
$product, $quantity,
$pricePerUnit, $totalPrice
);
echo $formatted_invoice;
?>
OutputInvoice:
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.
PHP
<?php
$products = [
["name" => "Laptop", "price" => 999.99, "quantity" => 2],
["name" => "Smartphone", "price" => 599.99, "quantity" => 3],
["name" => "Headphones", "price" => 99.99, "quantity" => 5],
];
// Print table headers
echo sprintf("| %-15s | %-10s | %-8s |\n", "Product", "Price", "Quantity");
echo "---------------------------------------\n";
foreach ($products as $product) {
echo sprintf("| %-15s | $%-9.2f | %-8d |\n", $product['name'], $product['price'], $product['quantity']);
}
echo "---------------------------------------\n";
?>
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
Similar Reads
PHP vsprintf() Function The vsprintf() function in PHP is an inbuilt function and used to display array values as a formatted string. The array elements will be inserted at the percent (%) signs in the main string. Display array values as a formatted string according to its format and accepts an array argument in place of
5 min read
PHP vprintf() function The vprintf() function in PHP is an inbuilt function which is used to display array values as a formatted string Display array values as a formatted string according to format it is work similar as printf() but accepts an array of arguments, in place of variables number of arguments. Returns the len
5 min read
PHP vfprintf() Function The vfprintf() function is an inbuilt function in PHP that is used to write formatted information to a stream, such as a file or the output screen. Syntax: vfprintf(resource $stream, string $format, array $values): Parameters: This function accepts three parameters that are described below. $stream:
2 min read
PHP | print_r() Function The print_r() function is a built-in function in PHP and is used to print or display information stored in a variable. Syntax: print_r( $variable, $isStore ) Parameters: This function accepts two parameters as shown in above syntax and described below. $variable: This parameter specifies the variabl
2 min read
PHP String Functions Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s
6 min read