Why Use Sprintf Function In Php Sprintf() Function, Php String Sprintf() Function

The arg1, arg2, ... Parameters will be inserted at percent (%) signs in the main string.

Bạn đang xem: Why use sprintf function in php sprintf() function, php string sprintf() function

This function works "step-by-step".

At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

If there are more % signs than arguments, you must use placeholders.

A placeholder is inserted after the % sign, và consists of the argument- number and "$". See example two.

Xem thêm: Cách Kê Gối Sau Khi Quan Hệ, Bật Mí Cách Kê Gối Để Dễ Thụ Thai Nhất


Parameter Values

Parameter Description
format Required. Specifies the string and how lớn format the variables in it.
arg1 Required. The argument khổng lồ be inserted at the first %-sign in the format string
arg2 Optional. The argument khổng lồ be inserted at the second %-sign in the format string
arg++ Optional. The argument khổng lồ be inserted at the third, fourth, etc. %-sign in the format string
format possible format values:

Item Description
%% 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 khổng lồ or greater than zero)
%f Floating-point number (local settings aware)
%F Floating-point number (not local settings aware)
%g shorter of %e và %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 % và the letter (example %.2f):

Item Description
+ Forces both + and - in front of numbers. By default, only negative numbers are marked
" Specifies what to lớn use as padding. Mặc định 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 khổng lồ 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.


Returns the formatted string


Using the format value %f:


"%f",$number);echo $txt;?>


Use of placeholders:




A demonstration of all possible format values:


// The ASCII Character 50 is 2// Note: The format value "%%" returns a percent signecho sprintf("%%b = %b",$num1)." "; // Binary numberecho sprintf("%%c = %c",$char)." "; // The ASCII Characterecho sprintf("%%d = %d",$num1)." "; // Signed decimal numberecho sprintf("%%d = %d",$num2)." "; // Signed decimal numberecho sprintf("%%e = %e",$num1)." "; // Scientific notation (lowercase)echo sprintf("%%E = %E",$num1)." "; // Scientific notation (uppercase)echo sprintf("%%u = %u",$num1)." "; // Unsigned decimal number (positive)echo sprintf("%%u = %u",$num2)." "; // Unsigned decimal number (negative)echo sprintf("%%f = %f",$num1)." "; // Floating-point number (ls aware)echo sprintf("%%F = %F",$num1)." "; // Floating-point number (not aware)echo sprintf("%%g = %g",$num1)." "; // Shorter of %e and %fecho sprintf("%%G = %G",$num1)." "; // Shorter of %E & %fecho sprintf("%%o = %o",$num1)." "; // Octal numberecho sprintf("%%s = %s",$num1)." "; // Stringecho sprintf("%%x = %x",$num1)." "; // Hexadecimal number (lowercase)echo sprintf("%%X = %X",$num1)." "; // Hexadecimal number (uppercase)echo sprintf("%%+d = %+d",$num1)." "; // Sign specifier (positive)echo sprintf("%%+d = %+d",$num2)." "; // Sign specifier (negative)?>


A demonstration of string specifiers:


"Hello";$str2 = "Hello world!";echo sprintf("<%s>",$str1)." "; // Stringecho sprintf("<%8s>",$str1)." "; // Right-justifies the string with spacesecho sprintf("<%-8s>",$str1)." "; // Left-justifies the string with spacesecho sprintf("<%08s>",$str1)." "; // Zero-paddingecho sprintf("<%"*8s>",$str1)." "; // Adds "*"echo sprintf("<%8.8s>",$str2)." "; // Left-justifies the string with spaces //(cuts off characters after the specified value)?>



PHP sha1() FunctionPHP similar_text() FunctionPHP soundex() FunctionPHP sscanf() FunctionPHP str_getcsv() Function