PHP vsprintf() Function

What is PHP vsprintf() function?

vprintf() function lets you return an array of values in your chosen format. For example, you want to add a plus or minus sign in front of a number like +1 or -1. Or, you want to add two zeros after decimal point like 1.00 etc. You can do all these with this function.

Syntax:

vsprintf(format, array)

Parameters:

The function has 2 required 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.

array (Required): It specifies an array whose elements act as arguments.Elements are the values that are to be formatted by the rules (or conversion specification) set in the placeholders.

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. flags: A flag consists of the following characters-
    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:

The function returns the formatted array of values.

Examples:

Example 1:

<pre>
<?php
$output = vsprintf("%s has %u states.", ["Unites States of America", 50]);
echo $output . "\n";
$output = vsprintf("The binary of %1\$d is %1\$b.", [5]);
echo $output . "\n";
$output = vsprintf("The hexadecimal of 10 is %x.", [10]);
echo $output . "\n";
$output = vsprintf("After Left-pads with zeros, the number \"12\" becomes: %05d", [12] );
echo $output . "\n";
$output = vsprintf("Adding +sign in front of a positive number 12 is: %+d", [12] );
echo $output . "\n";
$output = vsprintf("Adding trancating the string \"HiEmma\" to 2 characters, it becomes %.2s", ["HiEmma"] );
?>
</pre>

Output:

Unites States of America has 50 states.
The binary of 5 is 101.
The hexadecimal of 10 is a.
After Left-pads with zeros, the number "12" becomes: 00012
Adding +sign in front of a positive number 12 is: +12

Difference between vsprintf() and vprintf() function:

vprintf() function lets you return an array of values in your chosen format. On the other hand, vprintf() function lets you display a string in your chosen format.

PHP Version Support:

PHP4, PHP5, PHP7, PHP8

Summary: PHP vsprintf() Function:

vsprint() is a useful and powerful built-in PHP function. Use this function when you need to work with formatted output of an array of value.

Reference:

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