mjc Posted July 23, 2008 Share Posted July 23, 2008 I am both new here, and new to PHP (which I'm really loving ). I know PHP can help me with this, but can't quite figure it out. I would like to create a plain text file with every number in the range 1 - 1,000,000 (with commas placed correctly each third digit from end). Each number on a new line. The range should be easy to change. I have got this far, be this only gives one number, whereas I would like a file with every number, in order. <?php $content = ""; for($i=0;$i<100;$i++){ $content = rand(1,1000000) . "\n"; } $g = fopen("file.txt", 'a'); fwrite($g, $content); ?> If you have the time to help, I will be very grateful. If you can explain how the script works as well it will help me learn. thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/ Share on other sites More sharing options...
GingerRobot Posted July 23, 2008 Share Posted July 23, 2008 Try this: <?php $content = ""; for($i=1;$i<100;$i++){ $content .= number_format($i) . "\n"; } $g = fopen("file.txt", 'a'); fwrite($g, $content); ?> If you wanted them in order, you shouldn't be using rand(). The number_format() function will add the thousands separator for you. We use the .= operator to append the string to $content so it is not overwritten everytime. Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/#findComment-597701 Share on other sites More sharing options...
mjc Posted July 23, 2008 Author Share Posted July 23, 2008 thanks for the superfast reply ginger. That not only worked, but is way more elegant than js. can the intended number only be expressed as less than. so if 100 was the target <101? Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/#findComment-597720 Share on other sites More sharing options...
discomatt Posted July 23, 2008 Share Posted July 23, 2008 the '$i<100' part of the for loop and be any comparative operator... >, >=, <=, ==, ect. Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/#findComment-597722 Share on other sites More sharing options...
mjc Posted July 23, 2008 Author Share Posted July 23, 2008 wow that is clever. Can anyone recommend a good book or website where I can get down to learning properly... Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/#findComment-597755 Share on other sites More sharing options...
wildteen88 Posted July 23, 2008 Share Posted July 23, 2008 Best place is php.net and have read of the first few chapters of the documentation. Then head over to hudzilla.org which is a free php ebook. Quote Link to comment https://forums.phpfreaks.com/topic/116244-solved-use-php-to-write-a-large-txt-file/#findComment-597756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.