Home>

Php sprintf () function

Replace the percent sign (%) symbol with a variable passed as a parameter:

<?php
$number=2;
$str="shanghai";
$txt=sprintf ("there are%u million cars in%s.", $number, $str);
echo $txt;
?>

Definition and usage

The sprintf () function writes a formatted string into a variable.

The arg1, arg2, and ++ parameters are inserted at the percent (%) sign in the main string. This function is performed step by step.At the first%sign, insert arg1, at the second%sign, insert arg2, and so on.

Note:If the%sign is more than the arg parameter, you must use placeholders.The placeholder is after the%sign.Composed of numbers and "\ $".

grammar

parameter description
format

essential. Specifies the string and how to format the variables in it

Possible format values:

%%-returns a percent sign %b-binary number %c-character for ascii value %d-decimal number with negative sign (negative, 0, positive) %e-scientific notation in lower case (e.g. 1.2e + 2) %e-scientific notation in uppercase (e.g. 1.2e + 2) %u-decimal number without sign (greater than or equal to 0) %f-floating point number (local setting) %f-floating point number (non-local setting) %g-shorter%e and%f %g-shorter%e and%f %o-octal number %s-string %x-hexadecimal number (lowercase) %x-hexadecimal number (uppercase)

Additional format value.Must be placed between%and letters (e.g.%.2f):

+ (Prefix the number with + or-to define the sign.by default,Only negative numbers are marked,Positive numbers are not marked) "(Specify what to use as padding,The default is a space.It must be used with the width specifier.Example:%"x20s (use" x "as padding)) -(Left adjustment variable value) [0-9] (specify minimum width of variable value) . [0-9] (specify the number of decimal places or the maximum string length)

Note:If you use more than one of the above format values,They must be used in the above order.

arg1 essential. Requires insertion intoformatThe parameter at the first%sign in the string.
arg2 Optional. Requires insertion intoformatThe parameter at the second%sign in the string.
arg ++ Optional. Requires insertion intoformatArguments at the third and fourth percent signs in the string.

php printf () function

Output a formatted string:

<?php
$number=9;
$str="Beijing";
printf ("There are%u million bicycles in%s.
", $str, $number);
?>

Definition and usage

The printf () function outputs a formatted string.

grammar

printf (format, arg1, arg2, arg ++)

parameter description
format

essential. Specifies the string and how to format the variables in it

Possible format values:

%%-returns a percent sign %b-binary number %c-character for ascii value %d-decimal number with negative sign (negative, 0, positive) %e-scientific notation in lower case (e.g. 1.2e + 2) %e-scientific notation in uppercase (e.g. 1.2e + 2) %u-decimal number without sign (greater than or equal to 0) %f-floating point number (local setting) %f-floating point number (non-local setting) %g-shorter%e and%f %g-shorter%e and%f %o-octal number %s-string %x-hexadecimal number (lowercase) %x-hexadecimal number (uppercase)

Additional format value.Must be placed between%and letters (e.g.%.2f):

+ (Prefix the number with + or-to define the sign.By default, only negative numbers are marked.Positive numbers are not marked) "(Specify what to use as padding,The default is a space.It must be used with the width specifier.) -(Left adjustment variable value) [0-9] (specify minimum width of variable value) . [0-9] (specify the number of decimal places or the maximum string length)

Note:If you use more than one of the above format values,They must be used in the order above,Can't disrupt.

arg1 essential. Requires insertion intoformatThe parameter at the first%sign in the string.
arg2 essential. Requires insertion intoformatThe parameter at the second%sign in the string.
arg ++ Optional. Requires insertion intoformatArguments at the third, fourth, etc.%symbols in the string.

Here is an example:rounding to two decimal places

<?php
$num1=21;
echo sprintf ("%0.2f", $num1). "<br />";//prints 21.00
$num2=16.3287;
echo sprintf ("%0.2f", $num2). "<br />";//Output 16.33
$num3=32.12329;
echo sprintf ("%0.2f", $num3). "<br />";//output 32.12
?>

Explain the meaning of%0.2f:

%Means start character

0 means the spaces are filled with 0

2 means two digits after the decimal point

f for conversion to floating point

Conversion character

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

%Prints the percent sign.No conversion.

b Integer converted to binary.

c Integer converted to the corresponding ascii character.

d Integer converted to decimal.

F times precision converted to floating point.

o Integers are converted to octal.

s Integer into string.

x integers are converted to lowercase hexadecimal.

x integers are converted to uppercase hexadecimal.

The difference between printf and sprintf

1. printf function:

int printf (string format [, mixed args [, mixed ...]])
produces output according to format, which is described in the documentation for sprintf ().
returns the length of the outputted string.

Format the text and output it.Such as:

$name="hunte";
$age=25;
printf ("my name is%s, age%d", $name, $age);

2. sprintf function:

string sprintf (string format [, mixed args [, mixed ...]])
returns a string produced according to the formatting string format.

Similar to printf, but does not print,Instead, it returns the formatted text,Everything else is the same as printf.

3. print function:

Is a function that can return a value,There can be only one parameter.

int print (string arg)outputs arg. returns 1, always.
php
  • Previous Summary of jQuery data types (14)
  • Next Android background reminder function