2

我正在尝试在我的二进制代码前面添加一个 1 ,这就是我要做的事情:0101例如,如果我有 ,那么我会添加一个带有 4 个零的数字,比如 10000 所以它会变成 10101 .这是我的代码:

$fill = strlen($string);
$number = '1';
$add = str_pad($number, $fill, '0', STR_PAD_RIGHT);
$m1 = $string + $add;

问题是输出类似于1.random number e+Random number

4

2 回答 2

1

假设$string是你的"0101"字符串,你可以这样做$m1 = '1'.$string;

于 2012-12-15T21:05:51.270 回答
0

My previous answer was wrong because the length of the string is potentially variable and str_pad requires you to know the length. This will work, but it doesn't look so elegant:

if (strpos($string, '0') === 0) {
   $string = '1' . $string;
}
于 2012-12-15T21:08:39.560 回答