Embedded function usage of printf () for PHP string operations

Source: Internet
Author: User
The built-in function usage of printf () for PHP string operations php indicates that many of the most common print output functions are echo print () printf () spintf.


The echo and print () functions are the same, but print () has the return values (true and false) for successful execution ).


The functions printf () and sprintf () can also be used to implement more complex formatting output. The two methods are basically the same, but the printf () function is used to output a formatted string to the browser, and the sprintf () function is used to return a formatted string.


Basic usage of printf:

echo "you have $total money";

To use the printf () function to get the same result, use the following statement:

printf ("you have %s money", $total );

% S in the formatted string is the conversion tag. It means to replace yourself with the following variables. In this example, it is interpreted as a replacement of the string $ total. If the value in the $ total variable is 25.6, both of the above methods will print 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 be followed by two decimal points ). As follows:

printf("you hava %.2f money ", $total );

After this line of code formatting, 25.6 stored in $ total will be printed as 25.60. (But does not affect the original value of the variable)


Multiple conversion tags can be used in formatted strings. If there are n conversion tags, there should be n parameters after the formatted string. By default, each conversion tag is reformatted in the given order.

printf ("you have %.2f money , but shopping %.2f RMB ", $total , $total_shopping );

Here, the first conversion tag uses the value of $ total, and the second conversion tag uses the value of the variable $ total_shopping.


Printf () % conversion tag syntax format:

% ['Padding _ character] [-] [width] [. precision] type // the parameters in brackets are optional.

All the conversion tags start with %.

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

Character-is optional. It specifies the data alignment mode in the field. by default, this option is left blank, that is, the right alignment. if The-symbol is set, it is left alignment (fill in the character to the right not enough position ).

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

The precision parameter must start with a decimal point. It specifies the exact number of digits after the decimal point.

The last part of the conversion tag is a type code. All supported types are shown in the following table:

Code type Meaning
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 number.
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 an unspecified decimal number.
X

Interpreted as an integer with lowercase letters ~ Hexadecimal output of f

X Interpreted as an integer with uppercase letters ~ Hexadecimal output of F


Usage example:

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


When using the printf () function in the type conversion code, you can use the parameter method with serial numbers. This means that the order of parameters is not necessarily the same as that in the conversion mark. For example:

printf ("you have %2\$.2f money , but shopping %1\$.2f RMB ", $total , $total_shopping );

You only need to add the parameter position directly after the '%' symbol and end with the '$' symbol. In this example, 2 \ $ means to replace the second parameter in the list. This method can also be used in repeated parameters.

php > $a = 539;php > $b = 38;php > printf("you have %2\$.2f money , but shopping %1\$.2f RMB! ", $a,$b);you have 38.00 money , 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, instead of a variable number of parameters.

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.