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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP vsprintf() Function Usage and Example

   PHP String String Functions Manual

    The vsprintf() function is used to return a formatted string

Syntax

string vsprintf ( string $format , array $args )

Definition and Usage

It returns a formatted string

Unlike sprintf(), the parameters in vsprintf() 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.

Note:If there are more % symbols than arg parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of a number and "\$".

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

Return value

It returns the array value in the form of a formatted string

Parameter

NumberParameters and descriptions
1

format

Specify the string and how to format the variables within it

Possible format values:

  • %% - Return a percent sign %

  • %b - Binary number

  • %c - Character corresponding to ASCII value

  • %d - Decimal number with 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 sign (greater than or equal to 0)

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

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

  • %g - Short %e and %f

  • %G - Short %E and %f

  • %o - Octal number

  • %s - String

  • %x - Hexadecimal numbers (lowercase letters)

  • %X - Hexadecimal numbers (uppercase letters)

Additional format values. They 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-adjust the variable value)

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

  • ].[0-9] (Specify the number of decimal places or 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.

2

argarray

Specify the array to insert parameters

Online Example

Try the following example, formatting dates, leading zeros for integers

<?php
    print vsprintf("%04d-%02d-%02d", explode('-', ''1990-12-25);
   
    echo '<br>';
    //Using format value %f
    $num1 = 123;
    $num2 = 456;
    $txt = vsprintf("%f%f",array($num1$num2));
    echo $txt;   
?>
Test and see‹/›

Output Result

1990-12-25
123.000000456.000000

PHP String String Functions Manual