[PHP Learning Tutorial]007. Number 0 (Num padding)

Source: Internet
Author: User
Tags sprintf

Introduction: In the daily work, often need to use the number 0 of the operation, such as: Date format Yyyy-mm-dd and so on.

There are a number of pre-and post-fill functions in PHP-today, we introduce two commonly used, the implementation of the number 0:

    • Str_pad
    • sprintf

Please read the tutorial directly!

Fill function-str_pad

As the name implies, this function is for a string that can fill any other string with the specified string.

Syntax:str_pad(with filled string, fill the length, fill the string, fill the position)

Parameter resolution:

The length after filling-must be a positive integer

Fill position-There are three options:

    Left: Str_pad_left

    Right: Str_pad_right

Ends: Str_pad_both

Example 1:

Echo Str_pad (1,8, "0", str_pad_left);

Run result 1:

00000001

Example 2:

Echo Str_pad (1,8, "0", str_pad_right);

Run result 2:

10000000

Example 3:

Echo Str_pad (1,8, "0", Str_pad_both);

Run result 3:

00010000

Note: One of the notable details in the above example is that if the number of digits filled is an odd one, such as example three fills 7 0, the right priority. :)

Fill function-sprintf

1. Decimal supplement 0 +[after the decimal point 0]

Directly on the code:

Echo sprintf ("%01.3f", 1);

%01.3f means: a minimum of three digits after a decimal point is less than three 0, at least one before the decimal point, less than a zero of the floating-point number of the format behind the parameters

Run result 1:

1.000

Conclusion

On the complement of these several methods we can choose to use, in fact, each has the pros and cons,sprintf can ensure that you do not mistakenly operate it 1 1000000 haha,str_pad can guarantee you want to fill what.

Not finished, waiting to be updated ...

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4817667.html

[PHP Learning Tutorial]007. Number 0 (Num padding)

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.