【发布时间】:2018-09-28 04:25:22
【问题描述】:
我正在尝试获取给定数字的最大可用数。
例如:
$inputDigit1 = 4;
$inputDigit2 = 9;
$inputDigit3 = 1;
输出应该是:
$outputDigit1 = 9999;
$outputDigit2 = 999999999;
$outputDigit3 = 9;
现在这是我的代码:
$output = '';
for ($i=0; $i < $inputDigit; $i++) {
$output.='9';
}
$output = (int) $output;
但我不知道在不使用循环的情况下解决这种情况的正确或更简单的方法。
【问题讨论】:
-
为什么不创建一个数组而不是为每个值使用单个字符串? $inputDigit[1] = 4; $inputDigit2] = 9; $inputDigit[3] = 1;
-
亲爱的@Bernhard。我只是用这个来解释我的情况。哈哈
-
亲爱的@mickmackusa 感谢您的参考。我不知道之前是否有一个函数可以产生像 str_replace 这样的重复字符串。
-
现在你知道了。从现在开始,您将知道在提问之前应该广泛搜索 StackOverflow。通常,您的问题的答案已经存在于这里的数百万页中。