PHP | vsprintf() Function

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 formated 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 greather 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 formated string.

Additional format:

    • -> Left-justifies for variable value .
    • [0-9] -> maximum string length for number.
    • [0-9] -> minimum string width for variable value.
    • + -> Used for both[+ or-] (By default its negative number).

Program 1: String space specified program.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$str1 = "Geeks";
$str2 = "Geeksforgeeksarticle";
  
// print string-1 only 
echo vsprintf("%s\n", array(
    $str1
));
  
// print string-15 space
echo vsprintf("%15s\n", array(
    $str1
));
  
// print string-1 with space
echo vsprintf("%-25s\n", array(
    $str1
));
  
// print string with zero
echo vsprintf("%020s\n", array(
    $str1
));
  
// print string with * symbol
echo vsprintf("%'*10s\n", array(
    $str1
));
  
// print string-2 
echo vsprintf("%s\n", array(
    $str2
));
  
// print string-2 with decimal point
echo vsprintf("%2.10s\n", array(
    $str2
));
  
// print string-2 with space
echo vsprintf("%30s\n", array(
    $str2
));
  
// print string-2 with zero
echo vsprintf("%030s", array(
    $str2
));
  
  
?>

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// %f and %F floating number in 
// vsprintf function in php
$value1 = 789495321;
$value2 = 8080907021;
$value3 = 334422190;
  
  
echo "\n%f (local) Floating: ";
  
// for %f Floating-point number
// for local settings aware
  
$txt = vsprintf("%f %f %f", array(
    $value1,
    $value2,
    $value3
));
echo $txt;
  
echo "\n%F (Not local) Floating: ";
// for %F Floating-point number
// for local settings aware
  
$result = vsprintf("%F %F %F ", array(
    $value1,
    $value2,
    $value3
));
  
echo $result;
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// vsprintf function in php
// used %d, %u, %e, %E 
$value1 = 7894;
$value2 = 9070;
$value3 = 3344;
  
echo "%d Signed decimal number : ";
// %d Signed decimal number
// where (-, + or zero)
$txt = vsprintf("%d %d %d", array(
    $value1,
    $value2,
    $value3
));
echo $txt;
  
echo "\n%u UnSigned decimal number : ";
// %d UnSigned decimal number
// where (0<=zero)
  
$tt = vsprintf("%u %u %u", array(
    $value1,
    $value2,
    $value3
));
echo $tt;
  
echo "\n%e Scientific notation : ";
// Scientific notation for lowercase 
$result = vsprintf("%e %e %e ", array(
    $value1,
    $value2,
    $value3
));
echo $result;
  
echo "\n%E Scientific notation : ";
// Scientific notation for uppercase 
$result = vsprintf("%E %E %E ", array(
    $value1,
    $value2,
    $value3
));
echo $result;
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// vsprintf function in php
// used %%, %b, %o, %x, %X 
$value1 = 789495;
$value2 = 334455;
  
  
// Returns [%] sign
echo "% ->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[Uperercase]
echo "\n%X ->Hexadecimal number Uc : ";
$result = vsprintf("%X %X", array(
    $value1,
    $value2
));
echo $result;
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// vsprintf function in php
  
$value1 = 75;
$value2 = 55;
$char   = 97;
$char2  = 69;
  
// shorter of %e and %f
echo "%g shorter of %e and %f: ";
$txt = vsprintf("%g %g", array(
    $value1,
    $value2
));
echo $txt;
  
// %G – shorter of %E and %f 
echo "\n%G shorter of %E and %f : ";
  
$tt = vsprintf("%G %G", array(
    $value1,
    $value2
));
echo $tt;
  
// ASCII value
echo "\n%c ASCII value : ";
$result = vsprintf("%c %c ", array(
    $char,
    $char2
));
echo $result;
  
?>

chevron_right


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: http://php.net/manual/en/function.vsprintf.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.