This time it will appear: 001 If you want to increase the number of digits, you can increase 1000"/> This time it will appear: 001 If you want to increase the number of digits, you can increase 1000">

Home >Backend Development >PHP Tutorial >Phison-up10 mass production tool v1.78.00 PHP automatically adds 0 to the left side of numbers

Phison-up10 mass production tool v1.78.00 PHP automatically adds 0 to the left side of numbers

WBOY
WBOYOriginal
2016-07-29 08:37:471288browse

Copy the code The code is as follows:


echo "$newNumber" ; 
Example: If I want to add "4 zeros" in line 03, it will look like this.


Copy the code

The code is as follows:

$newNumber = substr(strval($sourceNumber+100000),1,5); ?>

In fact That is, how many total should be displayed? For digits, just add as many 0s after $sourceNumber+1, and the last number will be directly changed to display several digits.

Better way:
string str_pad (string $input, int $pad_length [, string $pad_string [, int $pad_type]] )


Copy the code

The code is as follows:

$ input = "Alien"; echo str_pad($input, 10); //produces "-=-=- Alien"                        str_pad($input, 10, "_", STR_PAD_BOTH);                                                                                                                                                                                                 produces produces "Alien_" ?>


Complete the length of the string. Use pad_string to pad. The default padding is on the right. If STR_PAD_LEFT, it is padded on the left. STR_PAD_BOTH is padded on both sides.
Use str_pad next time. After all, it is built-in and will definitely be faster than custom one.
/*
I don’t think your method above is very good. Let me introduce a method I wrote. The method function is as follows, so when you want the result 001, the method: dispRepair('1',3,'0')
Function: complement function
str: original string
type: type, 0 is complement, 1 Prepending
len: new string length
msg: padding characters
*/


Copy code

The code is as follows:


function dispRepair($str,$len,$msg,$type='1' ) {
$length = $len - strlen($str);
if($length<1)return $str;
if ($type == 1) {
$str = str_repeat($msg,$length). $str;
} else {

$str .= str_repeat($msg,$length); } return $str; }

The above introduces the phison-up10 mass production tool v1.78.00 PHP automatically adds 0 on the left side of the number, including the content of the phison-up10 mass production tool v1.78.00. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
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