printf () built-in function usage for PHP string manipulation

Source: Internet
Author: User
Tags uppercase letter
Many of the most common functions in PHP that indicate printouts are the echo print () printf () spintf ().


The Echo and print () functions are the same, but print () has a return value (True and false) for execution success or not.


Using the function printf () and sprintf () can also implement some more complex formatted output. The two work in the same way, except that the printf () function is to tell a formatted string output to the browser, whereas the sprintf () function returns a formatted string.


printf () Basic usage:

echo "You had $total money";

To get the same result using the printf () function, you should use the following statement:

printf ("You had%s money", $total);

The%s in the formatted string is a conversion token. It means to replace yourself with the following variables. In this example it will be interpreted as the substitution of the string $total. If the value in the $total variable is 25.6, both methods will be printed as 25.6.


The advantage of printf () is that you can use more useful conversion instructions to specify $total as a floating-point number (it should have a two-bit decimal point behind it). As shown below:

printf ("You Hava%.2f money", $total);

After this line of code is formatted, 25.6 stored in the $total is printed as 25.60. (but does not affect the original value of the variable)


You can use more than one conversion token in a formatted string. If there are n conversion tokens, then the format string should be followed by n parameters. Each conversion tag is reformatted by default in the order given.

printf ("You had%.2f money, but shopping%.2f RMB", $total, $total _shopping);

Here, the first conversion token will use the value of $total, and the second conversion tag will use the variable $total the value of _shopping.


The syntax format of the printf ()% Conversion token:

%[' padding_character] [-] [width] [. precision] Type//parameters in parentheses are optional parameters

All conversion marks start with a%.

The parameter padding_character will be used to fill the variable until the specified width. The function of this parameter is to add 0 in front of the number as if using a calculator. The default padding character is a space, and if you specify a space or 0, you do not need to use ' single quotation marks as a prefix. For any other padding characters, you must specify the ' single quotation mark prefix.

Character-is optional. It indicates the alignment of the data in the field, which is left-justified by default, with the-symbol set to the right (padding character fills to the right not enough).

The parameter width is optional, which tells the printf () function to display the character width (in characters) here.

The parameter precision must begin with a decimal point. It is used to indicate the exact number of digits after the decimal point.

The final part of the conversion token is a type code. All of its supported types are shown in the following table:

Code type Significance
B interpreted as an integer and output as a binary number
C interpreted as an integer and output as a character
D interpreted as an integer and output as a decimal
F interpreted as double precision and output as a floating-point number
O interpreted as an integer and output as an octal number
S interpreted as a string and output as a string
U interpreted as an integer and output as a non-specified decimal
X

interpreted as an integer and as a hexadecimal number output with a lowercase letter a~f

X interpreted as an integer and as a hexadecimal number output with an uppercase letter A~f


Usage examples:

php > $a = 534;p hp > printf ("printf type is%.1f", $a);p rintf type was 534.0php > printf ("printf type is% ' u.1f", $ A);p rintf type is 534.0php > printf ("printf type is% ' u6.1f", $a);p rintf type was u534.0php > printf ("printf type is% ' u10.1f ', $a);p rintf type is uuuuu534.0php > printf ("printf type was% ' u-10.1f", $a);p rintf type is 534.0uuuuuphp > pri NTF ("printf type is%b", $a);p rintf type are 1000010110php > printf ("printf type is%o", $a);p rintf type is 1026php > P rintf ("printf type is%s", $a);p rintf type are 534php > printf ("printf type is%u", $a);p rintf type is 534php > printf ( "printf type is%x", $a);p rintf type are 216php > $a = 539;php > printf ("printf type is%x", $a);p rintf type is 21b


When you use the printf () function in a type conversion code, you can use an ordinal argument, which means that the order of the parameters is not necessarily the same as in the conversion tag. For example:

printf ("You had%2\$.2f money, but shopping%1\$.2f RMB", $total, $total _shopping);

As long as the position of the parameter is added directly after the% symbol, and ends with the $ sign. In this example, 2\$ means replacing with the second parameter in the list. This method can also be used in repeating parameters.

php > $a = 539;php > $b = 38;php > printf ("You had%2\$.2f money, but shopping%1\$.2f rmb!", $a, $b); 38.00, but shopping 539.00 rmb! php >



There are two alternative versions of these functions, vprintf () and vsprintf (). These variant functions accept two parameters: A format string and a parameter array, rather than a variable number of arguments.

  • 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.