在PHP中,如何添加零填充数字字符串并保留零填充?

时间:2021-12-16 00:06:45

If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002.

如果我在PHP中有一个包含0001的变量,我向它添加1,结果是2而不是0002。

How do I solve this problem?

我该如何解决这个问题?

4 个解决方案

#1


$foo = sprintf('%04d', $foo + 1);

#2


It would probably help you to understand the PHP data types and how they're affected when you do operations to variables of various types. You say you have "a variable in PHP say 0001", but what type is that variable? Probably a string, "0001", since an integer can't have that value (it's just 1). So when you do this:

当您对各种类型的变量进行操作时,它可能有助于您理解PHP数据类型以及它们如何受到影响。你说你有“PHP中的变量说0001”,但那个变量是什么类型的?可能是一个字符串,“0001”,因为整数不能具有该值(它只是1)。所以当你这样做时:

echo ("0001" + 1);

...the + operator says, "Hm, that's a string and an integer. I don't know how to add a string and an int. But I DO know how to convert a string INTO an int, and then add two ints together, so let me do that," and then it converts "0001" to 1. Why? Because the PHP rules for converting a string to an integer say that any number of leading zeroes in the string are discarded. Which means that the string "0001" becomes 1.

... +运算符说,“嗯,这是一个字符串和一个整数。我不知道如何添加一个字符串和一个int。但我知道如何将字符串INTO转换为int,然后添加两个整数在一起,让我这样做,“然后它将”0001“转换为1.为什么?因为用于将字符串转换为整数的PHP规则表示字符串中的任意数量的前导零都将被丢弃。这意味着字符串“0001”变为1。

Then the + says, "Hey, I know how to add 1 and 1. It's 2!" and the output of that statement is 2.

然后+说,“嘿,我知道如何加1和1.它是2!”并且该陈述的输出为2。

#3


Another option is the str_pad() function.

另一个选项是str_pad()函数。

$text = str_pad($text, 4, '0', STR_PAD_LEFT);

#4


<?php
#how many chars will be in the string
$fill = 6;
#the number
$number = 56;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 000056
?>

#1


$foo = sprintf('%04d', $foo + 1);

#2


It would probably help you to understand the PHP data types and how they're affected when you do operations to variables of various types. You say you have "a variable in PHP say 0001", but what type is that variable? Probably a string, "0001", since an integer can't have that value (it's just 1). So when you do this:

当您对各种类型的变量进行操作时,它可能有助于您理解PHP数据类型以及它们如何受到影响。你说你有“PHP中的变量说0001”,但那个变量是什么类型的?可能是一个字符串,“0001”,因为整数不能具有该值(它只是1)。所以当你这样做时:

echo ("0001" + 1);

...the + operator says, "Hm, that's a string and an integer. I don't know how to add a string and an int. But I DO know how to convert a string INTO an int, and then add two ints together, so let me do that," and then it converts "0001" to 1. Why? Because the PHP rules for converting a string to an integer say that any number of leading zeroes in the string are discarded. Which means that the string "0001" becomes 1.

... +运算符说,“嗯,这是一个字符串和一个整数。我不知道如何添加一个字符串和一个int。但我知道如何将字符串INTO转换为int,然后添加两个整数在一起,让我这样做,“然后它将”0001“转换为1.为什么?因为用于将字符串转换为整数的PHP规则表示字符串中的任意数量的前导零都将被丢弃。这意味着字符串“0001”变为1。

Then the + says, "Hey, I know how to add 1 and 1. It's 2!" and the output of that statement is 2.

然后+说,“嘿,我知道如何加1和1.它是2!”并且该陈述的输出为2。

#3


Another option is the str_pad() function.

另一个选项是str_pad()函数。

$text = str_pad($text, 4, '0', STR_PAD_LEFT);

#4


<?php
#how many chars will be in the string
$fill = 6;
#the number
$number = 56;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 000056
?>