PHP | vprintf() function

The vprintf() function in PHP is an inbuilt function which is used to display array values as a formatted string
Display array values as a formatted string according to format it is work similar as printf() but accepts an array of arguments, in place of variables number of arguments. Returns the length of the outputted string on success.

Syntax:

vprintf (format, array_arguments)

Parameters:



  1. format: it is required parameter it specifies how will the string formated.

    Possible format values:

    • %% – 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
    • %E – Scientific notation using a uppercase (e.g. 1.2E+2)
    • %G – shorter of %E and %f
  2. array_arguments array arguments here for which need to format.


Program 1:
This program will be showing uses of % b d u x X f F o formats using vprintf function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$obj = new stdClass();
$obj->val1 = 9;
$obj->val2 = 10;
$obj->val3 = 15;
$obj->val4 = -1;
  
echo "using %% format: ";
  
// below is using of vprintf function
// for printing % format
vprintf('%% %% %% %%', $obj);
  
echo "\nusing %b format: ";
  
// below is using of vprintf function
// for format %b will print equivalent 
// binary number 
vprintf('%b %b %b %b', $obj);
  
echo "\nusing %d format: ";
  
// below is using of vprintf function
// for  %d format
vprintf('%d %d %d %d', $obj);
  
echo "\nusing %u format: ";
  
// below is using of vprintf function
// for  % u (unsigned decimal) format
vprintf('%u %u %u %u', $obj);
  
  
echo "\nusing %x format: ";
  
// below is using of vprintf function
// for  %x  Hexadecimal number (lowercase letters) format
vprintf('%x %x %x %x', $obj);
  
  
echo "\nusing %X format: ";
  
// below is using of vprintf function
// for  %X  Hexadecimal number (uppercase letters) format
vprintf('%X %X %X %X', $obj);
  
echo "\nusing %f format: ";
  
// below is using of vprintf function
// for  %f  Floating-point number (local settings aware)
vprintf('%f %f %f %f', $obj);
  
echo "\nusing %F format: ";
  
// below is using of vprintf function
// for  %F Floating-point number (not local settings aware)
vprintf('%F %F %F %F', $obj);
  
echo "\nusing %o format: ";
  
// below is using of vprintf function
// for  %o octal number
vprintf('%o %o %o %o', $obj);
  
?>

chevron_right


Output:

using %% format: % % % %
using %b format: 1001 1010 1111 1111111111111111111111111111111111111111111111111111111111111111
using %d format: 9 10 15 -1
using %u format: 9 10 15 18446744073709551615
using %x format: 9 a f ffffffffffffffff
using %X format: 9 A F FFFFFFFFFFFFFFFF
using %f format: 9.000000 10.000000 15.000000 -1.000000
using %F format: 9.000000 10.000000 15.000000 -1.000000
using %o format: 11 12 17 1777777777777777777777

Program 2: This program will be showing uses of c and s formats using vprintf function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$obj = new stdClass();
$obj->val1 = 65;
$obj->val2 = 66;
$obj->val3 = 97;
$obj->val4 = 98;
  
echo "using %c format: ";
  
// below is using of vprintf function
// for printing %c format will be print 
// ASCII character 
vprintf('%c %c %c %c', $obj);
  
echo "\nusing %s format: ";
  
// below is using of vprintf function
// for format %s will print as string 
vprintf('%s %s %s %s', $obj);
  
?>

chevron_right


Output:

using %c format: A B a b
using %s format: 65 66 97 98

Program 3: This program will be showing uses of e g E G formats using vprintf function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$obj = new stdClass();
$obj->val1 = 999999999;
$obj->val2 = 145956566;
$obj->val3 = 111111111;
$obj->val4 = 100000000;
  
echo "using %e format: ";
// below is using of vprintf function
// for printing %e format will be print 
// Scientific notation (lowercase) 
vprintf('%e %e %e %e', $obj);
  
echo"\nusing %g format: ";
// below is using of vprintf function
// for format %g will be print
// Shorter of %e and %f
vprintf('%g %g %g %g', $obj);
  
echo "\nusing %E format: ";
// below is using of vprintf function
// for format %E will print
// Scientific notation (uppercase)
vprintf('%E %E %E %E', $obj);
  
echo "\nusing %G format: ";
// below is using of vprintf function
// for format %G will be print
// Shorter of %E and %f
vprintf('%G %G %G %G', $obj);
  
?>

chevron_right


Output:

using %e format: 1.000000e+9 1.459566e+8 1.111111e+8 1.000000e+8
using %g format: 1.0e+9 1.45957e+8 1.11111e+8 1.0e+8
using %E format: 1.000000E+9 1.459566E+8 1.111111E+8 1.000000E+8
using %G format: 1.0E+9 1.45957E+8 1.11111E+8 1.0E+8

Program 4: In this program, all four variable will be print respectively 10 20 30 40 space separated using vprintf function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$obj = new stdClass();
$obj->val1 = 'gfg 1';
$obj->val2 = 'gfg 2';
$obj->val3 = 'gfg 3';
$obj->val4 = 'gfg 4';
  
// below is using of vprintf function
vprintf('%-10s %-20s %-30s %-40s', $obj);
  
?>

chevron_right


Output:

gfg 1      gfg 2                gfg 3                          gfg 4


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.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.