简体   繁体   中英

Generate a 10 digit number for n times

<?php
$rand = "06".mt_rand(0,9)."".mt_rand(0,9)."".mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);

print_r($rand); ?>

i want to use this code to generate 100 random numbers, then save the results in a txt file. I just don't know how to keep the code working until it reaches 100 digit numbers

Desired output

 0643895810
 0680345345
 0668104431
... 100 times

First you have to use a for loop . A loop allows you to do something so many times you want.

for ($i = 1; $i <= 100; $i++) {
}

$i++ is called Post-increment it just sets $i + 1. So it's like $i = $i + 1 .

Now put your random number string in the for loop.

<?php
    for ($i = 1; $i <= 100; $i++) {
    echo "06".mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9)."<br>";
    }
?>

Don't forget to add a <br> at the end. It's a line break. If you want to use it in a textfile you have to use \\n .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM