function sprintf () What is the role of

Source: Internet
Author: User
Tags sprintf
My PHP manual sprintf () is full of English, I can not understand. Is there any warrior who can take the function of the functions (especially the parameter format) to say it in detail?


Reply to discussion (solution)

Please Google translate a bit
%-literal percent semicolon. No parameters?? is required.
B-the argument is treated as an integer and as a binary number.
C-The receive parameter is treated as an integer and as a character with an ASCII value.
D-the argument is treated as an integer, as a (symbol) decimal number.
E-The parameter is considered scientific notation (e.g. 1.5e +3).
U-the parameter is treated as an integer and as an unsigned decimal number.
F-the parameter is considered floating point, as well as a floating-point number (locale-aware).
F-The parameter is considered floating point, as well as a floating-point number (non-locale aware). from PHP 4.3.10 and PHP 5.0.3.
O-the argument is treated as an integer and as an octal number.
S-the parameter is treated as a string.
X-the argument is treated as an integer, as a hexadecimal number (lowercase letter).
The X-parameter is treated as an integer, as a hexadecimal number (in uppercase letters).

Cases:
echo sprintf ('%b%c%d%u%e%f%f%o%s%x%x ', 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65);
1000001 A 6.500000e+1 65.000000 65.000000 101 65 41 41

Are there only floating-point types that have the "%.2f" parameter format?

Yes
%.2F is formatted as 2 decimal places

Integers are naturally not decimals.

The sprintf () function writes a formatted string to a variable.
sprintf (format,arg1,arg2,arg++)
Parameters
Format required. Conversion format.
Arg1 required. Specifies the parameter that is inserted into the first% symbol in the format string.
Arg2 is optional. Specifies the parameter that is inserted into the second% symbol in the format string.
arg++ is optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol.

 
  

Output:
Hello World. Day Number 123

Did anyone else add it?

What else is there to add?
1. sprintf is formatted output, which is output in the format you want, similar to the template

2. Unlike print, sprintf does not print directly to the screen, so you can use it to assign values to variables. such as $s = sprintf (.........);

W3school sprintf () Part of the sentence: If the% symbol is more than the arg parameter, you must use a placeholder. The placeholder is inserted after the% symbol and consists of a number and a "\$".
What does the number in the above sentence mean?

This is a unique way to use PHP, not much (the example he gave has been very clear)
It's a normal notation.

$a = 123; $b = 1111; $txt = sprintf ("%.2f  %u", $a, $b); Echo $txt;
123.00 1111
When writing
$txt = sprintf ("%2\$.2f  %1\ $u", $a, $b); Echo $txt;
1111.00 123
You can see that the value of the corresponding position has changed.
It is known that the number represents the ordinal number of the parameter

Thank you very much, knot.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.