How to Use PHP printf() Function for Formatting Strings
The printf() function in PHP is a built-in function used to format and output strings. In simple terms, it prints formatted strings.
Unlike the echo() or print() functions, which simply display data, the printf() function allows you to control the appearance of the output using format specifiers.
PHP printf() Function Syntax
In PHP, the printf() function outputs formatted strings. The general syntax to define printf() function in PHP is as follows:
printf(format, arg1, arg2, ...)
In the above syntax, the parameter format represents the string that is to be displayed and contains formatting specifiers. The format specifiers are special placeholders that start with a percent sign (%) following by a letter to indicate the type of value to be displayed. An example of format string is as follows:
"Name: %s, Age: %d"
In this formatted string, the %s is a format specifier used for a string, while %d is a format specifier used for a decimal integer.
The second set of parameters—such as arg1, arg2, and so on—are the actual values you want to display. These values are inserted into the format string in the same order as the corresponding format specifiers appear. Each argument replaces a format specifier in the specified order. For example:
<?php
printf("Name: %s, Age: %d", "Alice", 30);
?>
Output: Name: Alice, Age: 30
In this example, the format specifier %s is replaced by the string value “Alice”, while %d is replaced by the integer value 30.
Notes:
1. You must pass the values in the same sequence as the format specifiers.
2. The type of each argument value should match the type expected by the specifier.
PHP printf Formatting Specifiers
The formatting specifiers start with a “%” character, followed by a letter that indicates the type of value to be displayed. For example, a string is represented by %s. Here are some commonly used format specifiers in printf() function in PHP:
- %s – String
- %d – A signed decimal integer
- %u – An unsigned decimal integer
- %f – A floating point number
- %b – A integer represented as a binary number
- %c – A character based on the ASCII value
- %o – An octal number
- %x – Hexadecimal number in lowercase letters
- %X – Hexadecimal number in uppercase letters
- %% – Prints a percent sign.
- %e – Scientific notation using a lowercase (e.g. 1.2e+2)
- %E – Scientific notation using a uppercase (e.g. 1.2E+2)
Basic Examples of printf() Function in PHP
Example 1: PHP program to format a string using printf() function.
<?php
$name = "John";
printf("Hello, %s!", $name);
?>
Output: Hello, John!
In this example, we have used a format specifier %s for a string. It gets replaced by the value of $name.
Example 2: PHP program for formatting a string with an integer value.
<?php
$age = 25;
printf("Age: %d", $age);
?>
Output: Age: 25
Example 3: Floating Point Precision
<?php
$pi = 3.1415926535;
printf("Value of pi: %.2f", $pi);
?>
Output: Value of pi: 3.14
In this example code, we have used a format specifier %.2f that formats the float value to 2 decimal places.
Example 4: Padding with Zeros
<?php
$number = 45;
printf("Number: %05d", $number);
?>
Output: Number: 00045
In this example code, we have used a format specifier %05d which ensures the output is 5 characters long, padded with zeros on the left.
Example 5: Using %% to print a Percent Sign
<?php
$percentage = 85;
printf("Success rate: %d%%", $percentage);
?>
Output: Success rate: 85%
In this PHP code, we have defined a variable named $percentage and assigned it the integer value 85. Then, we used the format specifier %d, which tells PHP to insert an integer value at that position. In this case, it will be replaced by the value of $percentage, which is 85. After that, we used %% in the format string to print a literal percent sign (%) in the output.
Formatting Multiple Variables using PHP printf() Function
Example 6: PHP program to format a string with multiple variables using the printf() function.
<?php
$name = "Deepak";
$age = 28;
$city = "Dhanbad";
printf("Name: %s\nAge: %d\nCity: %s", $name, $age, $city);
?>
Output: Name: Deepak Age: 28 City: Dhanbad
In this PHP program, we have used two format specifiers: %s and %d. The %s specifier is used for the string values of the variables $name and $city, while %d is used for the integer value of the variable $age. The variables are inserted into the format string in the exact order in which the specifiers appear.
Formatting an Integer using printf() Function
Example 7: PHP Program to format an integer into binary, octal, and hexadecimal using printf() function.
<?php
$number = 255;
printf("Binary: %b\n", $number);
printf("Octal: %o\n", $number);
printf("Hex (lower): %x\n", $number);
printf("Hex (upper): %X\n", $number);
?>
Output: Binary: 11111111 Octal: 377 Hex (lower): ff Hex (upper): FF
Formatting Floating-Point Numbers as Currency
Example 8: PHP program to format a floating-point number as currency using printf() function
<?php
$price = 2570.5;
printf("Price: $%0.2f", $price);
?>
Output: Price: $2570.50
In this tutorial, you learned about the printf() function in PHP with the help of various examples. I hope you now have a clear understanding of how to use the printf() function to format strings and practiced all examples.