PHP vprintf() function

Example

Output the formatted string:

<?php
$number = 9;
$str = "Beijing";
vprintf("There are 쥕lion bicycles in %s.", array($number, $str));
?>

Run Examples

Definition and Usage

The vprintf() function outputs a formatted string.

Unlike printf(), the parameters in vprintf() are located in an array. 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.

Comment:If the % symbol is more than arg If parameters are provided, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and '\$'. See example 2.

Tip:Related functions: sprintf(), printf(), vsprintf(), fprintf(), and vfprintf()

Syntax

vprintf(format,argarray)
Parameter Description
format

Required. Specifies 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 - Uses lowercase scientific notation (for example 1.2e+2)
  • %E - Uses uppercase scientific notation (for example 1.2E+2)
  • 蒝ecimal number without a sign (greater than or equal to 0)
  • %f - Floating-point number (local setting)
  • %F - Floating-point number (non-local setting)
  • %g - Shorter than %e and %f
  • %G - Shorter than %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 a letter (for example %.2f):

  • + (adds a + or - in front of the number to define the sign of the number. By default, only negative numbers are marked, positive numbers are not marked)
  • ' (specifies what to use as padding, default is a space. It must be used with the width specifier. For example: '%x20s' (using 'x' as padding))
  • - (left aligns the variable value)
  • [0-9] (specifies the minimum width of the variable value)
  • .[0-9] (specifies the number of decimal places or the maximum string length)

Comment:If multiple format values are used, they must be in the order listed above.

argarray Required. An array with parameters, which will be inserted into format The %% symbol in the string.

Technical details

Return value: Returns the length of the output string.
PHP version: 4.1.0+

More examples

Example 1

Using format value %%f:

<?php
$num1 = 123;
$num2 = 456;
vprintf("%f%f",array($num1,$num2));
?>

Run Examples

Example 2

Using placeholders:

<?php
$number = 123;
vprintf("With two decimal places:%1$.2f<br>No decimal:%1$u",array($number));
?>

Run Examples

Example 3

Demonstrate all possible format values using printf():

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2
// Comment: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // ASCII character
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%d = %d <br>",$num2); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase)
printf("%鑾u <br>",$num1); // Unsigned decimal number (positive)
printf("%鑾u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (respecting the local settings)
printf("%%F = %F <br>",$num1); // Floating-point number (not respecting the local settings)
printf("%%g = %g <br>",$num1); // Shorter than %%e and %%f
printf("%%G = %G <br>",$num1); // Shorter than %%E and %%f
printf("%%o = %o <br>",$num1); // Octal Number
printf("%%s = %s <br>",$num1); // String
printf("%%x = %x <br>",$num1); // Hexadecimal Number (Lowercase)
printf("%%X = %X <br>",$num1); // Hexadecimal Number (Uppercase)
printf("%%+d = %+d <br>",$num1); // Positive Sign Specifier
printf("%%+d = %+d <br>",$num2); // Negative Sign Specifier
?>

Run Examples

Example 4

Demonstration of String Specifiers:

<?php
$str1 = "Hello";
$str2 = "Hello world!";
vprintf("[%s]<br>",array($str1));
vprintf("[%8s]<br>",array($str1));
vprintf("[%-8s]<br>",array($str1));
vprintf("[%08s]<br>",array($str1));
vprintf("[%'*8s]<br>",array($str1));
vprintf("[%8.8s]<br>",array($str2));
?>

Run Examples