PHP printf() Function

What is PHP printf() function?

printf() function lets you display a string in your chosen format. For example, you want to add a plus or minus sign in front of a number like +100 or -100. Or, you want to add two zeros after decimal point like 200.00. You can do all these with this function.

Syntax:

printf(format, argument1, argument2, …)

Parameters:

format (Required): In this parameter, mention the string that you want to format. It contains zero or more placeholders. Each placeholder defines a rule (or conversion specification) to format the value that will be inserted in it by an argument.

argument1, argument2…: Arguments are the values that are to be formatted by the rules (or conversion specification) set in the placeholders. Argument1 is formatted by the 1st placeholder’s rule, Argument2 is formatted by the 2nd placeholder’s rule and so on.

A conversion specification has the following syntax-

%[argnum$][flags][width][.precision]specifier.

  • %: Each conversion specification starts with percentage sign, %.
  • argnum: it is an integer followed by the dollar sign $ that tells which argument number to be used. Ex. $1 means first argument, $2 means second argument. So, you can change the regular order of the arguments that are to be used in the placeholders. Or you can repeat arguments if you wish.
  • flags: A flag consists of the following characters-
    It left-justify within the given field width. See Example 1 (line 2). Right-justification is the default.
    +It adds plus sign (+) in front of a positive number. See example 1 (line 3).
    (space)It pads the result with spaces. See example 1 (line 4).
    0It adds zero at the left of a number. If you use s specifier, it adds 0 at right too. See example 1 (line 4)
    ‘(char)It pads the result with the character (char). See example 1 (line 5)
  • width: After conversion, how many characters (minimum) should be produced is indicated by the width which is an integer number. 1 due.
  • .precision: It is a combination of a period followed by a integer like (.2) or a period followed by * (.*). It’s meaning depends on the specifier to be used next.  it Is very useful for floating point formatting.
    For e, E, f and F specifiers:When it is used with these specifiers, it tells how many digits to be displayed after decimal number (6 is the default).
    For g, G, h and H specifiers:When it is used with these specifiers, it tells how many significant digits to be displayed.
    For s specifierWhen it is used with s specifiers, it tells after how many characters the string will be cutoff.
  • specifier: This is a single character that indicates by which type the argument is treated and presented.
    %It displays a percent sign %.
    bIt treats a parameter (argument1, argument2, …) as a integer and displays it as binary number.
    cIt treats a parameter (argument1, argument2, …) as a integer and displays it as an ASCII character equivalent value.
    dIt treats a parameter (argument1, argument2, …) as a integer and displays it as a signed decimal number.
    eIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in lowercase.
    EIt treats a parameter (argument1, argument2, …) as a scientific notation and displays it in uppercase.
    fIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (locale setting dependent)
    FIt treats a parameter (argument1, argument2, …) as a float and displays it as a floating-point number (not locale setting dependent)
    gGeneral format. It uses e and f
    GGeneral format like g, but, It uses E and f.
    hGeneral format like g, but, It uses F. Available at PHP 8.0.0.
    HGeneral format like g, but, It uses E and F. Available at PHP 8.0.0.
    oIt treats a parameter (argument1, argument2, …) as a integer and displays it as an octal number.
    sIt treats a parameter (argument1, argument2, …) as a string and displays it as a string.
    uIt treats a parameter (argument1, argument2, …) as a integer and displays it as an unsigned decimal number.
    xIt treats a parameter (argument1, argument2, …) as a integer and displays it as a lowercase hexadecimal number.
    XIt treats a parameter (argument1, argument2, …) as a integer and displays it as a uppercase hexadecimal number.

Return Values:

Returned the length of the formatted string that you get from the format parameter.

Examples:

Example 1: Changing Arguments order.

<?php
// Changing Arguments order.
printf("%1\$d and %2\$d make %3\$s", 2, 3, "Six"); // Output: 2 and 3 make Six
?>

Example 2: Getting number of element of a multidimensional array –

<pre>
<?php
// Left-justifies the string value with spaces
printf("[%-10s]", "PHP"); // Output: [PHP       ]
// Right-justifies the string with spaces.
printf("[%+10s]","PHP"); // Output: [PHP       ]
?>
</pre>
<?php
// Adding + sign in front of a number
printf("%+d", 100);   // Output: +100
// Left-pads a number with zeros.
printf("%05d", 123); // Output: 00123
// Right-pads a string with zeros.
printf("%05s", "PHP"); // Output: 00123 // NOT WORKING
// Pads a string with @ character
printf("%'@5s", "PHP"); // Output: @@PHP
// Default precision of 6 digits decimal number after decimal point.
printf( "%f", 34.567 );      // Output: 34.567000
// Displaying a floating number with 2 digits after decimal number.
printf( "%.2f", 34.567 );  // Output: 34.57
// Precision point with s specifier
printf( "%.13s", "PHP Functions abc" );  // Output: PHP Functions
// Displaying % sign
printf( "printf is top 5%% used functions in PHP.");  // Output: printf is top 5% used functions in PHP.
?>

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP printf() Function

One of the most frequently used PHP built-in string functions.

Reference:

https://www.php.net/manual/en/function.printf.php