PHP vsprintf() Function

PHP vsprintf() Function

The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments.

Syntax

vsprintf ( string $format , array $args )

Parameter

format(required)- This parameter specifies the string and how to format the variables in it.

The conversion specification of this parameter follows the given prototype: %[flags][width][.precision]specifier.

Flags

Flag Description
- Left-justify within the given field width and by default, it follows the right justification
+ It signifies the prefix positive numbers with a plus sign (+). By default, only negative is prefixed with a negative sign.
(space) It pads the result with spaces. This is the default.
0 It only left-pads numbers with zeros. With “s” specifiers this can also right-pad with zeros.
(char) It pads the result with the character (char).

Width

It signifies an integer that states how many characters (minimum) this conversion should result in.

Precision

  • e, E, f and F specifiers: The number of digits to be printed after the decimal point (by default, it is 6).
  • g and G specifiers: The maximum number of significant digits to be printed.
  • s specifier: It acts as a cutoff point, setting a maximum character limit to the string.

If the period is specified without an explicit value for precision, 0 is assumed.

Specifiers

Specifier Description
% It represents a literal percent character, and no other argument is required.
B In this, the argument is treated as an integer and presented as a binary number.
C In this, the argument is treated as an integer and presented as the character with that ASCII.
D In this, the argument is treated as an integer and presented as a (signed) decimal number.
E In this, the argument is treated as scientific notation. The precision specifier stands for the number of digits after the decimal point.
E It is like the “e” specifier, but it uses the uppercase letter.
F The argument is treated as a float and presented as a floating-point number (locale aware).
F The argument is treated as a float and presented as a floating-point number (non-locale aware). 
G It represents a general format.
G Unlike the “g” specifier but it also uses E and F.
O In this, the argument is treated as an integer and presented as an octal number.
S In this, the argument is treated and presented as a string.
U In this, the argument is treated as an integer and presented as an unsigned decimal number.
X The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X The argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

Type Handling

Type Specifiers
String S
Integer d, u, c, o, x, X, b
Double g, G, e, E, f, F

args(optional)- This parameter represents an array with arguments inserted at the third, fourth, etc. %-sign in the format string.

Return

This function returns the array values after formatting the specified string according to the given format or return FALSE on failure.

Example 1

 

Output

321.000000654.000000

Example 2

 

Output

%c = 2

Example 3

 

Output

[Hello]
[Hello PHP]
[***Hello]
[Hello PHP]
[000Hello]
[Hello PH] 

Example 4

 

Output

The unsigned decimal number for 126789 is:126789
The unsigned decimal number for -126789 is:18446744073709424827
The Floating-point number for 126789 is:126789.000000
The Floating-point number for -126789 is:-126789.000000
The Shorter value of %e and %f 126789 is:126789
The Shorter value of %E and %f -126789 is:18446744073709424827 

Related Topics

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP array_push() Function

PHP array_push() Function The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed. Syntax array_push ( array &$array, mixed& value [, mixed $... ]...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.