Home > Backend Development > PHP Tutorial > Detailed description of php printf function formatting (1/2)_PHP tutorial

Detailed description of php printf function formatting (1/2)_PHP tutorial

WBOY
Release: 2016-07-20 11:02:06
Original
1036 people have browsed it

The printf() function outputs a formatted string.

Grammar
printf(format,arg1,arg2,arg++) parameter description
format required. Specifies a string and how to format variables within it.
arg1 required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2 optional. Specifies the parameter to be inserted at the second % sign in the format string.
arg++ Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.

Description
Arguments arg1, arg2, ++, etc. are inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.
Tips and Notes
Note: If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % sign and consists of a number and "$". See example 3

The complete format of printf’s format control:
% - 0 m.n l or h format character
The items that make up the format description are explained below:
①%: Indicates the starting symbol of the format description, which is indispensable.
②-: If - means left-aligned output, if omitted, it means right-aligned output.
③0: If there is 0, it means that the specified blank is filled with 0. If it is omitted, it means that the specified blank is not filled.
④m.n: m refers to the field width, that is, the number of characters occupied by the corresponding output item on the output device. n refers to precision. The number of decimal places used to describe the output real number. When n is specified, the implicit precision is n=6 digits.
⑤l or h: l refers to long type for integer type and double type for real type. h is used to correct the format character of integer type to short type.

————————————————————————————————————————————————————————
Format character
Format characters are used to specify the data type and output format of the output item.
①d format: used to output decimal integers. There are several usages:
%d: Output according to the actual length of integer data.
%md: m is the width of the specified output field. If the number of digits in the data is less than m, spaces will be added to the left end. If it is greater than m, the actual number of digits will be output.
%ld: Output long integer data.
②o format: Output integers in unsigned octal form. Long integers can be output in "%lo" format. You can also specify the field width to output in "%mo" format.
Example:
main()
{ int a = -1;
printf("%d, %o", a, a);
}
Running result: -1,177777
Program analysis: -1 in the memory unit (stored in complement form) is (1111111111111111)2, and converted to octal number is (177777)8.
③x format: Output integers in unsigned hexadecimal form. Long integers can be output in "%lx" format. You can also specify the field width to output in "%mx" format.
④u format: Output integers in unsigned decimal form. Long integers can be output in "%lu" format. You can also specify the field width to output in "%mu" format.

1 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445388.htmlTechArticleprintf() function outputs a formatted string. Syntax printf(format,arg1,arg2,arg++) Parameter Description format Required. Specifies a string and how to format variables within it. arg1 required. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template