Get max number from given digit [PHP]

990 Views Asked by At

I'm trying to get the max number available from given digit.

For example :

$inputDigit1 = 4;
$inputDigit2 = 9;
$inputDigit3 = 1;

And the output should be :

$outputDigit1 = 9999;
$outputDigit2 = 999999999;
$outputDigit3 = 9;

For now this is my code :

$output = '';
for ($i=0; $i < $inputDigit; $i++) { 
    $output.='9';
}
$output = (int) $output;

But I don't know the proper or simpler way to solve this case without use a loop.

2

There are 2 best solutions below

0
Wils On BEST ANSWER
<?php
$string='9';
$repeat=9;
echo str_repeat($string,$repeat);

//output 999999999

You can set number of time the input string should be repeated.

0
Lawrence Cherone On

Without a loop, use str_repeat()

<?php
$inputDigit1 = 4;
echo str_repeat($inputDigit1, $inputDigit1); // 4444

Though numbers like 999999999 would cause memory issues.