Phpprintf () outputs the formatted string-small_123

Source: Internet
Author: User
Phpprintf () outputs the formatted string-small_123 php printf () function used to output the formatted string. This article introduces the usage and basic usage 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++)

  

Parameters Description
Format

Required. Specifies the string and how to format the variable.

Possible format values:

  • %-Return a percent sign %
  • % B-binary number
  • Characters corresponding to % c-ASCII values
  • % D-decimal number (negative, 0, positive) containing positive and negative numbers)
  • % E-use scientific notation in lower case (for example, 1.2e + 2)
  • % E-scientific notation in upper case (for example, 1.2E + 2)
  • % U-decimal number that does not contain positive and negative numbers (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 values
  • % S-string
  • % X-hexadecimal (lowercase letter)
  • % X-hexadecimal (uppercase letters)

Additional format value. Must be placed between % and letters (for example, %. 2f ):

  • + (Add + or-before the number to define the positive and negative of the number. By default, only negative numbers are marked, and positive numbers are not marked)
  • '(Specifies what to fill with. the default value is space. It must be used with the specified width .)
  • -(Adjust the variable value on the left)
  • [0-9] (minimum width of variable value)
  • . [0-9] (specifying the number of decimal places or the maximum string length)

Note: If you use multiple of the preceding format values, they must be used in the above order and cannot be disrupted.

Arg1 Required. Required to be insertedFormatThe parameter at the first % symbol in the string.
Arg2 Required. Required to be insertedFormatThe parameter at the second % sign in the string.
Arg ++ Optional. Required to be insertedFormatThe third, fourth, and other % parameters in the string.

Technical details
Return value: Returns the length of the output string.
PHP version: 4 +

Instance

Example 1

Use format value % f:

 

Online operation

Example 2

Use placeholders:

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

Online operation

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)?>

Online operation

Example 4

Demonstration of string specifiers:

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

Online operation

Address: http://www.manongjc.com/article/867.html

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.