What is the role of the printf() function in PHP?

The printf() function in PHP is used to display a formatted string in the browser. It allows you to insert variables into a string using format specifiers instead of just concatenating strings together.

Basic syntax:

printf( format, value1, value2, ... );

Common format specifiers:

  • %s — string
  • %d — integer
  • %f — float
  • %05d — integer padded with zeros to 5 digits

Let’s see it in action with a simple example:

PHP printf() example assigning the string

Below is the same example with string concatenation instead:

Comparison between PHP string concatenation and printf(), showing how printf() handles multiple variables more cleanly using %s and %d placeholders.

In WordPress specifically you’ll often see it used with esc_html__() for translatable, escaped strings:

WordPress PHP example using printf() with esc_html__() and pagination variables to safely display text like “Page X of Y”.
Real WordPress code example using printf() with esc_html__() and pagination variables to safely display pagination.

This is a common WordPress pattern because it keeps the translatable string intact as one unit — translators see "Page %d of %d" rather than fragmented pieces split by concatenation.

An example of basic text formatting in PHP using printf()

Here is a practical example covering the most common formatting scenarios:

Practical PHP printf() example formatting an order summary with variables for product name, quantity, price, order ID, and formatted date output.

Explanation

The code begins by declaring five variables that represent the details of a customer order. $product holds the name of the item as a string, $price stores its unit price as a floating-point number, $quantity is an integer representing how many units were ordered, and $order_id is a simple integer identifier for the order.

The $timestamp variable is generated using PHP’s built-in mktime() function. This function accepts time and date components in the order: hour, minute, second, month, day, year. Here mktime(0, 0, 0, 3, 7, 2025) produces a Unix timestamp (an integer counting seconds since January 1, 1970) representing midnight on March 7, 2025.

The printf() function then formats and outputs the order summary. It works like a template engine; the first argument is a format string containing special placeholders, and each subsequent argument provides the value to substitute into the respective placeholder.

Here is what each placeholder does:

%05d — formats $order_id as an integer, padded with leading zeros to a minimum width of 5 digits, so 7 becomes 00007.
%s — inserts $product as a plain string (Widget).
%d — inserts $quantity as an integer (12).
%.2f — formats $price as a floating-point number with exactly 2 decimal places (4.90). Note the $ before %.2f in the string is a literal dollar sign character, not a PHP variable.
The final %s receives the result of date("F j, Y", $timestamp), which converts the Unix timestamp into a human-readable date string like March 7, 2025.
The final output would look like this:

Order #00007 — Widget x12 @ $4.90 each — Placed: March 7, 2025

printf vs sprintf

sprintf is a native PHP function (also found in many other languages like C, JavaScript, etc.) whose name stands for “string print formatted“. Instead of outputting text directly to the page, it returns a formatted string that you can store in a variable or pass into another function.

It works by taking a format string as its first argument, which contains literal text mixed with special placeholders that begin with %. Each placeholder is then replaced, in order, by the additional arguments you pass to the function.

Compared to printf(), the only difference is that sprintf() returns the string instead of outputting it.

Since sprintf() returns the formatted string but does not output it you need to echo (or print) that returned string to the output buffer (HTML/CLI).

Exmple of echoing the formatted string created with sprintf() function.

The sprintf() function is useful when you want to store or further manipulate the result before displaying it.

This line writes the contents of the $message variable into a file named log.txt using PHP’s built-in file_put_contents() function. Here, the $message value is returned by sprintf().

Conclusion

The printf() function in PHP is an effective tool for formatting and displaying text in a specific format. It allows us to create strings that are formatted with placeholders for variable values.

Although my blog doesn’t support comments, feel free to reply via email or X.