A deep understanding of phpprintf () output formatted strings

Source: Internet
Author: User
The following small series will bring you an in-depth understanding of phpprintf () output formatted strings. I think it is quite good. now I will share it with you and give you a reference. Let's take a look at the php printf () function for outputting formatted strings. This article introduces the usage and basic use examples of the php printf () function to the coders, for more information, see.

Definition and usage

The printf () function outputs formatted strings.

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

Note: If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted into the % symbol, it consists of numbers and "\ $. See Example 2.

Tip: Related functions: sprintf (), vprintf (), vsprintf (), fprintf (), and vfprintf ()

• Fprintf ()
• Sprintf ()
• Vfprintf ()
• Vprintf ()
• Vsprintf ()

Syntax

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

Instance

Example 1

Use format value % f:

 

Example 2

Use placeholders:

 No decimals: % 1 \ $ u ", $ number);?>

Example 3

Demonstration of all possible format values:

 ", $ Num1); // binary number printf (" % c = % c
", $ Char); // ASCII character printf (" % d = % d
", $ Num1); // signed decimal number printf (" % d = % d
", $ Num2); // signed decimal number printf (" % e = % e
", $ Num1); // Scientific notation (lower case) printf (" % E = % E
", $ Num1); // Scientific notation (uppercase) printf (" % u = % u
", $ Num1); // unsigned decimal number (positive) printf (" % u = % u
", $ Num2); // unsigned decimal number (negative) printf (" % f = % f
", $ Num1); // floating point number (depending on local settings) printf (" % F = % F
", $ Num1); // floating point number (not depending on local settings) printf (" % g = % g
", $ Num1); // shorter than % e and % fprintf (" % G = % G
", $ Num1); // less than % E and % fprintf (" % o = % o
", $ Num1); // eight digits printf (" % s = % s
", $ Num1); // string printf (" % x = % x
", $ Num1); // hexadecimal number (lower case) printf (" % X = % X
", $ Num1); // hexadecimal number (uppercase) printf (" % + d = % + d
", $ Num1); // symbol specifier (positive) printf (" % + d = % + d
", $ Num2); // symbol specifier (negative)?>

Example 4

Demonstration of string specifiers:

 ",$str1);printf("[%8s]
",$str1);printf("[%-8s]
",$str1);printf("[%08s]
",$str1);printf("[%'*8s]
",$str1);printf("[%8.8s]
",$str2);?>

The above in-depth understanding of php printf () output formatted strings is all the content shared by the editor. I hope to give you a reference and support for PHP.

For more details about phpprintf () output formatted strings, please follow the PHP Chinese network!

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.