PHP7 Tutorial

PHP7 HOME PHP7 Intro PHP7 Install PHP7 Syntax PHP7 Variables PHP7 Echo / Print PHP7 Data Types PHP7 Strings PHP7 Constants PHP7 Operators PHP7 If...Else...Elseif PHP7 Switch PHP7 While Loops PHP7 For Loops PHP7 Functions PHP7 Arrays PHP7 Sorting Arrays PHP7 Superglobals

PHP7 Forms

PHP7 Form Handling PHP7 Form Validation PHP7 Form Required PHP7 Form URL/E-mail PHP7 Form Complete

PHP7 Advanced

PHP7 Arrays Multi PHP7 Date and Time PHP7 Include PHP7 File Handling PHP7 File Open/Read PHP7 File Create/Write PHP7 File Upload PHP7 Cookies PHP7 Sessions PHP7 Filters PHP7 Filters Advanced

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Delete Data MySQL Update Data MySQL Limit Data


PHP7 XML Parsers PHP7 SimpleXML Parser PHP7 SimpleXML - Get PHP7 XML Expat PHP7 XML DOM



PHP7 Reference

PHP7 Overview PHP7 Array PHP7 Calendar PHP7 Date PHP7 Directory PHP7 Error PHP7 Filesystem PHP7 Filter PHP7 FTP PHP7 Libxml PHP7 Mail PHP7 Math PHP7 Misc PHP7 MySQLi PHP7 Network PHP7 SimpleXML PHP7 Stream PHP7 String PHP7 XML Parser PHP7 Zip PHP7 Timezones

PHP 7 vsprintf() Function

❮ PHP String Reference


Write a formatted string to a variable:

$number = 9;
$str = "Beijing";
$txt = vsprintf("There are %u million bicycles in %s.",array($number,$str));
echo $txt;
Try it Yourself »

Definition and Usage

The vsprintf() function writes a formatted string to a variable.

Unlike sprintf(), the arguments in vsprintf(), are placed in an array. The array elements will be inserted at the percent (%) signs in the main string. This function works "step-by-step". At the first % sign, the first array element is inserted, at the second % sign, the second array element is inserted, etc.

Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$". See example two.

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


Parameter Description
format Required. Specifies the string and how to format the variables in it.

Possible format values:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)

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

argarray Required. An array with arguments to be inserted at the % signs in the format string

Technical Details

Return Value: Returns array values as a formatted string
PHP Version: 4.1.0+

More Examples


Using the format value %f:

$num1 = 123;
$num2 = 456;
$txt = vsprintf("%f%f",array($num1,$num2));
echo $txt;
Try it Yourself »


Use of placeholders:

$number = 123;
$txt = vsprintf("With 2 decimals: %1\$.2f
<br>With no decimals: %1\$u",array($number));
echo $txt;
Try it Yourself »


Using sprintf() to demonstrate all possible format values:

$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
echo sprintf("%%b = %b",$num1)."<br>"; // Binary number
echo sprintf("%%c = %c",$char)."<br>"; // The 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 = %u",$num1)."<br>"; // Unsigned decimal number (positive)
echo sprintf("%%u = %u",$num2)."<br>"; // Unsigned decimal number (negative)
echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (local settings aware)
echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not local sett aware)
echo sprintf("%%g = %g",$num1)."<br>"; // Shorter of %e and %f
echo sprintf("%%G = %G",$num1)."<br>"; // Shorter of %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>"; // Sign specifier (positive)
echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative)
Try it Yourself »


A demonstration of string specifiers:

$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>";
Try it Yourself »

❮ PHP String Reference