PHP vsprintf() Function

Example

Write the formatted string into a variable:

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

Run Instance

Definition and Usage

The vsprintf() function writes the formatted string into a variable.

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 If there are parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of a number and '\$'. See example 2.

Tip:Related functions:

Syntax

vsprintf(format,argarray)
Parameters 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 - Use lowercase scientific notation (e.g., 1.2e+2)
  • %E - Use uppercase scientific notation (e.g., 1.2E+2)
  • 蒝ecimal number without a sign (greater than or equal to 0)
  • %f - Floating-point number (local settings)
  • %F - Floating-point number (non-local settings)
  • %g - Shorter %e and %f
  • %G - Shorter %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

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

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

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

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

Technical details

Return value: Returns the array values in the form of a formatted string.
PHP version: 4.1.0+

More examples

Example 1

Use the format value %f:

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

Run Instance

Example 2

Using placeholders:

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

Run Instance

Example 3

Use sprintf() to demonstrate all possible format values:

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

Run Instance

Example 4

Demonstration of String Literals:

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

Run Instance