How to format the printf () function in PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Detailed description of formatting of the printf () function in PHP. The advantage of the printf () function is that it can format the output! Format: % [padding_character] [-] [width] [. precision] all type conversions start with %. to print a % sign, the printf () function must be used to format the output!
Format: % ['padding _ character] [-] [width] [. precision] type
All conversions start with %. to print a % symbol, you must use %.
The 'padding _ character parameter is optional. it will be used to fill the variable until the specified width. this parameter is filled before the variable. the default padding character is a space. if 0 or space is specified, you do not need to use 'single quotation marks as the prefix. Otherwise, you must specify 'as the prefix.
Parameter-optional. it indicates the left alignment. the default value is the right alignment.
The width parameter refers to the length of the variable to be replaced.
The precision parameter indicates the number of digits to be displayed after the decimal point.
The type parameter is a type code. See the following table:
Type | meaning
B | interpreted as an integer and output as binary.
C | interpreted as an integer and output (ASCII code) as a character ).
D | interpreted as an integer and output as an integer.
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 and output as a hexadecimal number with a lowercase letter a-f
X | interpreted as an integer and output as a hexadecimal number with an upper-case A-F

Demo:
Bytes ---------------------------------------------------------------------------------------------
$ Str = "0758 jian ";
$ StrA = "";
$ StrB = "B ";
$ Num1 = 5;
$ Num2 = 5;
$ Num3 = 0.25;
$ Num4 = 3.2567;
$ Num5 = 8;
$ Num6 = 1.735;
$ Num7 = 16777215;
$ Num8 = 16777215;
Printf ("% 2 \ $ s % 1 \ $ s", $ strA, $ strB); // 2 \ $ is the specified parameter location
Echo'
';
Printf ("fill: % '% 10 s", $ str); // specify the padding character to % string width to 10
Echo'
';
Printf ("binary: % B", $ num1 );
Echo'
';
Printf ("ASCII code: % c", $ num2 );
Echo'
';
Printf ("integer: % d", $ num3 );
Echo'
';
Printf ("floating point: %. 2f", $ num4 );
Echo'
';
Printf (": % o", $ num5 );
Echo'
';
Printf ("string: % s", $ str );
Echo'
';
Printf ("non-decimal: % u", $ num6 );
Echo'
';
Printf ("hexadecimal: % x", $ num7 );
Echo'
';
Printf ("hexadecimal: % X", $ num8 );
?>
Bytes ---------------------------------------------------------------------------------------------
Output result:
Bytes ---------------------------------------------------------------------------------------------
B
Fill: % 0758 jian
Binary: 101
ASCII code:
Integer: 0
Floating point: 3.26
Octal: 10
String: 0758 jian
Non-decimal: 1
Hexadecimal: ffffff
Hexadecimal: FFFFFF

The advantage of the http://www.bkjia.com/PHPjc/364176.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364176.htmlTechArticleprintf () function is that the output can be formatted! Format: % ['padding _ character] [-] [width] [. precision] all type conversions start with %. to print a % symbol, you must...

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.