Home » PHP vsprintf() Function

PHP vsprintf() Function

by Online Tutorials Library

PHP String vsprintf() Function

The vsprintf() function is an in-built function of PHP, which is used to display the array values as a formatted string. It works similar to the sprintf() function, but it accepts an array of arguments rather a variable number of arguments. PHP 4.1.0 and above versions support this function.

In this function, the elements of the array along with the percent (%) signs in the main string. It displays the array values as the formatted string according to its format. It returns the formatted string.

There are some other functions, which are similar to the vsprintf().

Related functions

vfprintf(),
vprintf(),
sprintf(),
printf(), are some similar functions to the vsprintf().

Syntax

The syntax of the vsprintf() function is given below, which accepts two parameters, and both are mandatory.

Parameters

$format (required) – It is a mandatory parameter of vsprintf() function, which specifies how to format a variable in it.

Possible format specifiers values:

  • %% – A literal % character, no argument is required
  • %b – Represented as a binary number
  • %c – Display characters according to the ASCII values.
  • %d – Represented as a signed decimal number.
  • %e – The arguments are treated as scientific notation using the lowercase letter (e.g. 3.2e+2)
  • %E – Similar to e specifier but uses uppercase (e.g. 3.2E+2)
  • %u – unsigned decimal number
  • %f – represented as a floating-point number (locale aware)
  • %F – Also represented as a floating-point number but non-locale aware
  • %g – Shorter of %e and %f
  • %G – Shorter of %E and %F
  • %o – Represented as an octal number
  • %s – Treated as well as represented as a string
  • %x – Represented as a hexadecimal number with lowercase letters
  • %X – Represented as a hexadecimal number but with uppercase letters

Warning: The c specifier ignores the width and padding.

Additional format values:

  • Right justification is by-default and left-justify within the given field width.
  • + Prefix the positive number with + sign, by default only negative numbers are marked with -ive sign.
  • Pads the result with the character.
  • (space) Pads the result with spaces.
  • 0 Only left-pads numbers with zeros. With the s specifier, it can also right-pads with zeros.

$array_args (required)– This is also a mandatory parameter of this function. It specifies an array containing the arguments to be inserted at the % signs in the format string.

Return values

The vsprintf() function returns the formatted string.

Examples

Below is the list of examples, by which we can learn the implementation of the vsprintf() function.

Example 1

Output:

3542.000000   9324.000000  

Example 2

Output:

My enrollment number is: 3215  

You may also like