Friday, September 27, 2024
Google search engine
HomeLanguagesPHP sprintf() Function

PHP sprintf() Function

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;Ā Ā Ā 
Ā Ā 
?>


Output

Name: 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;Ā Ā Ā 
Ā Ā 
?>


Output

Invoice:
Product: Widget
Quantity: 10
Price per Unit: $24.95
Total Price: $249.50

Reference: https://www.php.net/manual/en/function.sprintf.php

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?