PHP sprintf() Function

The sprintf() function writes a formatted string to a variable.

string sprintf ( string $format [, mixed $args [, mixed $... ]] )

The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Returns a string produced according to the formatting string format.

Example -

Example #1 Argument swapping

This will output "There are 5 monkeys in the tree". But imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:

Example #2 Argument swapping

We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:

Example #3 Argument swapping

An added benefit here is that you can repeat the placeholders without adding more arguments in the code. For example:

Example #4 Argument swapping

When using argument swapping, the n$ position specifier must come immediately after the percent sign (%), before any other specifiers, as shown in the example below.

Example #5 Specifying padding character

The above example will output:

......123

000000123

Example #6 Position specifier with other specifiers

Example #7 printf(): various examples

The above example will output:

%b = '10100111101010011010101101' %c = 'A' %d = '43951789' %e = '4.39518e+7' %u = '43951789' %u = '4251015507' %f = '43951789.000000' %o = '247523255' %s = '43951789' %x = '29ea6ad' %X = '29EA6AD' %+d = '+43951789' %+d = '-43951789'

Example #8 printf(): string specifiers

The above example will output:

[monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke]

Example #9 sprintf(): zero-padded integers

Example #10 sprintf(): formatting currency

Example #11 sprintf(): scientific notation

ParameterDescription
formatRequired. Specifies the string and how to format the variables in it.
Possible format values:
  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • T%e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)
  • Additional format values. These are placed between the % and the letter (example %.2f):
  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)
  • arg1Required. The argument to be inserted at the first %-sign in the format string
    arg2Optional. The argument to be inserted at the second %-sign in the format string
    arg++Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string

    Returns a string produced according to the formatting string format, or FALSE on failure.