PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals

PHP Forms

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

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP JSON


PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Traits PHP Static Methods PHP Static Properties

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 Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data


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



PHP Examples

PHP Examples PHP Quiz PHP Exercises PHP Certificate

PHP Reference

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

PHP 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 Values

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