Home > Software design >  How do I repeat the serial numbers from 000 to 999 in php and save it in a txt file [closed]
How do I repeat the serial numbers from 000 to 999 in php and save it in a txt file [closed]

Time:09-26

How do I repeat the serial numbers from 000 to 999 in php and save it in a txt file

For example, I have this code

    <?php    
$bb=fopen('log.txt','w');
for ($i=0;$i<=999;$i  ) 
    echo str_pad($i,5,"0",STR_PAD_LEFT),"\n","<br/>";
fwrite($bb,"$i");
fclose($bb);
?>

I want to make it like this

    <?php    
$bb=fopen('log.txt','w');
for ($i=0;$i<=999;$i  ) 
    $mm = str_pad($i,5,"0",STR_PAD_LEFT),"\n","<br/>";
fwrite($bb,"$mm");
fclose($bb);
?>

CodePudding user response:

Your code sames be good, but have some issue. First you should initialize $mm by empty string, then in loop you can use . to concatenating all numbers.

The other problem is ,"\n","<br/>": you changes this line from echo, and caused this issue, simply replace , by '.' to solve this one.

$bb = fopen('log.txt', 'w');
$mm = '';
for ($i = 0; $i <= 999; $i  )
{
    $mm .= str_pad($i, 5, "0", STR_PAD_LEFT) . "\n";
}
fwrite($bb, "$mm");
fclose($bb);
  •  Tags:  
  • php
  • Related