PHP vsprintf() Function (original) (raw)

Last Updated : 11 Jul, 2025

The vsprintf() function in PHP is an inbuilt function and used to display array values as a formatted string. The array elements will be inserted at the percent (%) signs in the main string. Display array values as a formatted string according to its format and accepts an array argument in place of variable number of arguments. The function returns formatted string. while vprintf() outputs a formatted string
Syntax:

vsprintf (format, arr_arguments)

Parameters Used : This Function takes Two parameter which are described below-

  1. format: It has Required parameter. It Specifies how string formatted to be variables. Possible format values as below:
    • %% - Returns a percent sign
    • %b - Binary number
    • %d - Signed decimal number (negative, zero or positive)
    • %u - Unsigned decimal number (equal to or greater than zero)
    • %x - Hexadecimal number (lowercase letters)
    • %X - Hexadecimal number (uppercase letters)
    • %f - Floating-point number (local settings aware)
    • %F - Floating-point number (not local settings aware)
    • %o - Octal number
    • %c - The character according to the ASCII value
    • %s - String
    • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
    • %g - shorter of %e and %f
    • %G - shorter of %E and %f
    • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  2. arr_arguments: An array arguments inserted at the % signs formatted string.

Additional format:

Program 1: String space specified program.

php `

`

Output:

Geeks Geeks Geeks
000000000000000Geeks *****Geeks Geeksforgeeksarticle Geeksforge Geeksforgeeksarticle 0000000000Geeksforgeeksarticle

Program 2: Drive floating program %f and %F in vsprintf() function in PHP.

php `

`

Output:

%f (local) Floating: 789495321.000000 8080907021.000000 334422190.000000 %F (Not local) Floating: 789495321.000000 8080907021.000000 334422190.000000

Program 3: Implement %d, %u, %e and %E in vsprintf() function.

php `

`

Output:

%d Signed decimal number : 7894 9070 3344 %u UnSigned decimal number : 7894 9070 3344 %e Scientific notation : 7.894000e+3 9.070000e+3 3.344000e+3 %E Scientific notation : 7.894000E+3 9.070000E+3 3.344000E+3

Program 4: Implement %%, %b, %o, %x, and %Xin vsprintf() function in PHP.

php `

Returns [%] sign: "; $txt = vsprintf("%% %%", array( $value1, $value2 )); echo $txt; // Returns [%b] binary number echo "\n%b ->binary number : "; $tt = vsprintf("%b %b", array( $value1, $value2 )); echo $tt; // Returns [%o] octal number echo "\n%o ->octal number : "; $result = vsprintf("%o %o ", array( $value1, $value2 )); echo $result; // Returns [%x] Hexadecimal number[lowercase] echo "\n%x ->Hexadecimal number Lc : "; $result = vsprintf("%x %x", array( $value1, $value2 )); echo $result; // Returns [%X] Hexadecimal number[Uppercase] echo "\n%X ->Hexadecimal number Uc : "; $result = vsprintf("%X %X", array( $value1, $value2 )); echo $result; ?>

`

Output:

% ->Returns [%] sign: % % %b ->binary number : 11000000101111110111 1010001101001110111 %o ->octal number : 3005767 1215167 %x ->Hexadecimal number Lc : c0bf7 51a77 %X ->Hexadecimal number Uc : C0BF7 51A77

**Program 5:**Implement %g %G and %c(ASCII) vsprintf() function in PHP.

php `

`

Output:

%g shorter of %e and %f: 75 55 %G shorter of %E and %f : 75 55 %c ASCII value : a E

Related Article: PHP | vprintf() function
References: https://www.php.net/manual/en/function.vsprintf.php