Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ​ PHP to fill in zeros before a specified number

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use PHP to fill zero in front of a specified number", the content of the explanation in the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use PHP to fill zero in front of a specified number"!

PHP adds zeros to the front of the number and zero to fixed digits.

When designing a unique number, you need a fixed number of digits, such as a fixed four-digit format:

For example:

The 0001 after YSZC, the following set of numbers, how to make it self-increasing?

1-> 000156-> 0056288-> 02881992-> 1992

We can use the php built-in function str_pad () to populate the string to a new length.

Str_pad (string,length,pad_string,pad_type) / / parameter description string / / required. Specifies the string to fill. Length / / required. Specifies the new string length. If the value is less than the original length of the string, nothing is done. Pad_string / / optional. A string that specifies the use of padding. The default is blank. Pad_type / / optional. Specifies which side of the padding string. / / possible values: STR_PAD_BOTH-/ / fill both sides of the string. If it is not an even number, the right side gets an additional fill. STR_PAD_LEFT-/ / fill the left side of the string. STR_PAD_RIGHT-/ / fill the right side of the string. Default.

For example, I wrote a method to determine whether there is any data in the data table, initialize 0001 if not, otherwise call self-incrementing ID.

/ * * @ desc Agreement No. * @ author Leifeng * @ Date on 2021-12-1 16:23 * @ param $num * @ return string * / public function inc_privacy_no ($num) {$start = "0001"; $data = Table:: find ('id'); / / here is the latest sql data in the query table. Write if (empty ($data)) {return $num.$start } $data_no = str_pad (($data ['id'] + 1), 4, "0", STR_PAD_LEFT); return $num.$data_no;}

Call method

$this- > inc_privacy_no ("YSZC") Thank you for your reading, the above is the content of "how to use PHP to fill zero in front of the specified number". After the study of this article, I believe you have a deeper understanding of how to use PHP to fill zero in front of the specified number, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report