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-
- 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)
- arr_arguments: An array arguments inserted at the % signs formatted 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.
php
<?php$str1 = "Geeks";$str2 = "Geeksforgeeksarticle";// print string-1 onlyecho vsprintf("%s\n", array( $str1));// print string-15 spaceecho vsprintf("%15s\n", array( $str1));// print string-1 with spaceecho vsprintf("%-25s\n", array( $str1));// print string with zeroecho vsprintf("%020s\n", array( $str1));// print string with * symbolecho vsprintf("%'*10s\n", array( $str1));// print string-2echo vsprintf("%s\n", array( $str2));// print string-2 with decimal pointecho vsprintf("%2.10s\n", array( $str2));// print string-2 with spaceecho vsprintf("%30s\n", array( $str2));// print string-2 with zeroecho vsprintf("%030s", array( $str2));?> |
Geeks
Geeks
Geeks
000000000000000Geeks
*****Geeks
Geeksforgeeksarticle
Geeksforge
Geeksforgeeksarticle
0000000000Geeksforgeeksarticle
Program 2: Drive floating program %f and %F in vsprintf() function in PHP.
php
<?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;?> |
%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
<?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;?> |
%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
<?php// vsprintf function in php// used %%, %b, %o, %x, %X$value1 = 789495;$value2 = 334455;// Returns [%] signecho "% ->Returns [%] sign: ";$txt = vsprintf("%% %%", array( $value1, $value2));echo $txt;// Returns [%b] binary numberecho "\n%b ->binary number : ";$tt = vsprintf("%b %b", array( $value1, $value2));echo $tt;// Returns [%o] octal numberecho "\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;?> |
% ->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
<?php// vsprintf function in php$value1 = 75;$value2 = 55;$char = 97;$char2 = 69;// shorter of %e and %fecho "%g shorter of %e and %f: ";$txt = vsprintf("%g %g", array( $value1, $value2));echo $txt;// %G – shorter of %E and %fecho "\n%G shorter of %E and %f : ";$tt = vsprintf("%G %G", array( $value1, $value2));echo $tt;// ASCII valueecho "\n%c ASCII value : ";$result = vsprintf("%c %c ", array( $char, $char2));echo $result;?> |
%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


