A deep understanding of php printf () output formatted strings and a deep understanding of printf

Source: Internet
Author: User

A deep understanding of php printf () output formatted strings and a deep understanding of printf

The php printf () function is used to output formatted strings. This article describes how to use the php printf () function and the basic examples. 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:

<?php $number = 123; printf("%f",$number); ?> 

Example 2

Use placeholders:

<? Php $ number = 123; printf ("two decimal places: % 1 \ $. 2f <br> no decimal places: % 1 \ $ u", $ number);?>

Example 3

Demonstration of all possible format values:

<? Php $ num1 = 123456789; $ num2 =-123456789; $ char = 50; // the ASCII character 50 is 2 // note: format value "%" Return percentage printf ("% B = % B <br>", $ num1 ); // binary number printf ("% c = % c <br>", $ char); // ASCII character printf ("% d = % d <br> ", $ num1); // The signed decimal number printf ("% d = % d <br>", $ num2 ); // signed decimal number printf ("% e = % e <br>", $ num1); // scientific notation (lower case) printf ("% E = % E <br>", $ num1); // scientific notation (uppercase) printf ("% u = % u <br> ", $ num1); // unsigned decimal number (positive) pri Ntf ("% u = % u <br>", $ num2); // unsigned decimal number (negative) printf ("% f = % f <br>", $ num1); // floating point number (depending on local settings) printf ("% F = % F <br> ", $ num1); // floating point number (not depending on local settings) printf ("% g = % g <br>", $ num1 ); // shorter than % e and % f printf ("% G = % G <br>", $ num1 ); // less than % E and % f printf ("% o = % o <br>", $ num1 ); // eight bytes printf ("% s = % s <br>", $ num1); // string printf ("% x = % x <br> ", $ num1); // hexadecimal number (lower case) printf ("% X = % X <br>", $ num1); // hexadecimal number (upper case) printf ("% + D = % + d <br> ", $ num1); // symbol specifier (positive) printf (" % + d = % + d <br> ", $ num2); // symbol specifier (negative)?>

Example 4

Demonstration of string specifiers:

<?php$str1 = "Hello";$str2 = "Hello world!";printf("[%s]<br>",$str1);printf("[%8s]<br>",$str1);printf("[%-8s]<br>",$str1);printf("[%08s]<br>",$str1);printf("[%'*8s]<br>",$str1);printf("[%8.8s]<br>",$str2);?>

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

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.