Javatpoint Logo
Javatpoint Logo

PHP String vprintf() Function

The vprintf() is an in-built string function of PHP. It is used to display the array values as a formatted string according to the format. The main purpose of this function is to display a formatted string. PHP 4 and above versions supports vprintf() string function.

The vprintf() function works similar to the printf() function, but it accepts an array of arguments. These array elements are inserted along with percent (%) signs in the main string. The vprintf() function works step-by-step. At each % sign, elements are inserted. It returns the length of the outputted string after successful execution.

Note: If % signs are more than the argument passed in the function, then we must use the placeholder. A placeholder is always inserted after the % sign, and contains the argument such as - number and "$".

Unlike vfprintf() function, it does not contains $stream argument that where to write the string.

Related functions

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

Syntax

The syntax of the vprintf() is given below, in which both arguments are mandatory to pass in this function.

Parameters

$format (required) - It is a mandatory parameter of vprintf() function, which specifies that how a string is formatted.

Possible format values:

  • %% - It returns a percent symbol
  • %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

Additional format values:

These values are placed between the % sign and the letters, e.g., %.2f

  • + = Prefix both + and - sign, by default only negative numbers are marked with -ive sign.
  • - = Left-justify within the given variable value, Right justification is by default.
  • ' = 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.

Note: If multiple additional format values are used, then they must be in the same order as above.

$array_arg (required) - These are the array arguments which need to format. The arguments within an array are to be inserted at the % symbol in the format string.

Return values

$array_arg (required) The vprintf() function returns the length of the outputted string.

Examples

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

Example 1

In the below example, we will open a test.txt file in write mode ("w") to write some data in it. If test.txt file does not exist, it will automatically create the file in the same folder, and then we can write the text in it.

Output:

Unlike vprintf(), it displays the string data directly on the browser, rather than in a file.

There are 365 days in 1 year.

Example 2: Use of placeholder

Output:

In the above example, we have used the placeholder here, because % is more than the arguments passed in the function.

With two decimal point: 12.35 
With three decimal point: 12.350 
With no decimal: 12

The following data will be written in the test.txt file.

512.000000
18446744073709551370





Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA