PHP Printf and Sprintf Statements


In this tutorial, you will learn how to use the PHP printf() and sprintf() statements to show the output in a web browser.


The PHP printf() Statement

The printf() statement is ideal when you want to create a static text and a mix of dynamic data stored in one or more variables. This is ideal for two reasons. First of all, it clearly defines the stable and dynamic data in two distinct parts, which gives easy maintenance. Second, printf() allows you to manage a lot of control over the type of printer, type, specification, alignment, and dynamic information in the location. Its prototype looks like this:

integer printf(string format ,[mixed args])

The following example will show how to a single dynamic integer value into an otherwise static string with the printf() statement:

Code Example:
<?php
   printf("CoderWell website visited %d times.", 91);
?>

Executing this command produces the following:

CoderWell website visited 91 times.

In this example, %d is a placeholder known as a type specifier, and d indicates that an integer value will be placed at that position. When the printf() statement is executed, the only argument, 91, will be included in the placeholder.

Keep in mind that an integer is expected, so if you pass by passing a number with a decimal value (known as float), it will round to the nearest integer if you exceed 91.2 or 91.6 then 91 will output.

Similar arguments apply to other type specifications (see Table for a list of the commonly used spacer).

Type specifications:

Type Description
%b Arguments considered an integer; Is presented as a binary number
%c Argument considered an integer; Is presented as a character corresponding to that ASCII value
%d Argument considered an integer; Is presented as a signed decimal number
%f Argument considered a floating-point number; Is presented as a floating-point number
%o Argument considered an integer; Is presented as an octal number
%s Argument considered a string; Is presented as a string
%u Argument considered an integer; Is presented as an unsigned decimal number
%x Argument considered an integer; Is presented as a lowercase hexadecimal number
%X Argument considered an integer; Is presented as an uppercase hexadecimal number

So if you want to pass along two values? Simply insert two specifiers into the string and confirm that you pass two values along the arguments as well.

The following example will show how to passes in an integer and float value with the printf() statement:

Code Example:
<?php
   printf("%d bottles of soda water cost $%f", 100, 42.30); 
?>

Executing this command produces the following:

100 bottles of soda water cost $42.300000

Since it is not an ideal financial representation, when working with decimal values, you can adjust precision using a precision specifier. An example follows:

Code Example:
<?php
   printf("$%.2f", 42.3); 
?>

Executing this command produces the following:

$42.30

The PHP sprintf() Statement

The sprintf() statement is functionally identical to printf() except that the output is assigned to a string instead of presentation in the browser. The prototype follows:

string sprintf(string format ,[mixed arguments])

An example follows:

Code Example:
<?php
   $str = "coderwell.com";
   $vtime = 11;
   $txt = sprintf("%s website visited %u times.",$str,$vtime);
   echo $txt;
?>

Executing this command produces the following:

coderwell.com website visited 11 times.

Note : If there are more% marks than the arguments, you have to use placeholders. A placeholder is inserted after% sign, and the argument number and “\$