English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP vfprintf() Function Usage and Example

   PHP String String Functions Manual

    The vfprintf() function is used to write formatted strings to a specified output stream (such as a file or database).

Syntax

vfprintf(stream, format, argarray)

Definition and Usage

It is used to convert formatted strings to a specific output

Different from fprintf(), the parameters in vfprintf() are located in an array. The array elements will be inserted into the main string at the percentage (%) symbol. This function is executed step by step. At the first % symbol, the first array element is inserted, at the second % symbol, the second array element is inserted, and so on.

Note:If there are more % symbols than arg parameters, you must use placeholders. Placeholders are inserted after the % symbol, composed of numbers and "\$". See the example 2.

Tip:Related functions:fprintf(), printf(),sprintf(),vprintf() andvsprintf()

Return value

 Returns the length of the output string.

Parameter

NumberParameters and descriptions
1

stream

Required. Specify where to write/Output string

2

format

Required. Specify the string and how to format the variables within it

Possible format values:

  • %% - Returns a percentage sign %

  • %b - Binary number

  • %c - Character corresponding to ASCII value

  • %d - Decimal number with a sign (negative, 0, positive)

  • %e - Use lowercase scientific notation (for example 1.2e+2

  • %E - Use uppercase scientific notation (for example 1.2E+2

  • 㩵n - Decimal number without a sign (greater than or equal to 0)

  • %f - Floating-point number (local settings)

  • %F - Floating-point number (non-local settings)

  • %g - Short %e and %f

  • %G - Short %E and %f

  • %o - Octal number

  • %s - String

  • %x - Hexadecimal number (lowercase letters)

  • %X - Hexadecimal number (uppercase letters)

Additional format values. Must be placed between % and the letter (for example %.2f):

  • + (Add + Or - To define the sign of the number. By default, only negative numbers are marked, and positive numbers are not marked)

  • ' (Specify what to use as the fill, the default is a space. It must be used with the width specifier. For example: %'x20s(Use "x" as the fill)

  • - (Left align the variable value)

  • [0-9](Specify the minimum width of the variable value)

  • ][0-9](Specify the number of decimal places or the maximum string length)

Note:If multiple of the above format values are used, they must be used in the order specified above and cannot be mixed.

3

argarray

Required. Used to specify the array of parameters to be inserted, which will be inserted into the format string at the % symbol position.

Online Example

Try the following example, write some text to a file:

<?php
   //Write some text to a file:
   $input1 = 123;
   $input2 = 456;
   $file = fopen("sample.txt","r");
   
   vfprintf($file,"%f%f",array($input1$input2));
?>

PHP String String Functions Manual