tyra Posted March 21, 2009 Share Posted March 21, 2009 How to set comma in fwrite output like this ? <?php $filea = fopen('filea.txt', 'wb'); $fileb = fopen('fileb.txt', 'wb'); for($i = 1; $i <= 100; $i++) { if($i % 2) fwrite($filea, $i); else fwrite($fileb, $i); } fclose($filea); fclose($fileb); ?> Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/ Share on other sites More sharing options...
rhodesa Posted March 21, 2009 Share Posted March 21, 2009 what do you mean? how do you put a comma between the numbers? fwrite($filea, $i . ','); Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790108 Share on other sites More sharing options...
tyra Posted March 21, 2009 Author Share Posted March 21, 2009 sry i got confused back there. actually i ment how to get comma between every fourth number that it creates. Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790118 Share on other sites More sharing options...
Maq Posted March 21, 2009 Share Posted March 21, 2009 Maybe I'm confused but, by judging on what you're asking you're a couple things wrong... 1) If you want every fourth number why are you using modulus 2? 2) Why do you write to 2 files? 3) This should put a comma between every fourth number, from 1 to 100. $filea = fopen('filea.txt', 'wb'); for($i=1; $i{ if(($i % 4) == 0) fwrite($filea, $i . ', '); else fwrite($filea, $i. ' '); } fclose($filea); ?> If I'm mistaken on what you're trying to accomplish please, give more detail. Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790135 Share on other sites More sharing options...
tyra Posted March 21, 2009 Author Share Posted March 21, 2009 output numbers must be like this: 1357, 9111315, 171921.. and so on. same thing wiht even numbers. Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790145 Share on other sites More sharing options...
Maq Posted March 21, 2009 Share Posted March 21, 2009 So is filea even numbers and every fourth number separated by a comma? And fileb vice versa? Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790149 Share on other sites More sharing options...
sasa Posted March 21, 2009 Share Posted March 21, 2009 <?php $filea = fopen('filea.txt', 'wb'); $fileb = fopen('fileb.txt', 'wb'); $sep = ''; for($i = 1; $i <= 100; $i++) { if($i % 2) fwrite($filea, $i.$sep); else { if($i % 8 == 0) $sep = ', '; else $sep = ''; fwrite($fileb, $i. $sep); } } fclose($filea); fclose($fileb); ?> Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790170 Share on other sites More sharing options...
Maq Posted March 21, 2009 Share Posted March 21, 2009 Yes, sasa's is correct but it will only delimit every fourth number with a comma if the numbers are consecutive from 1 to 100, just in case this was an example you were trying to apply to something else. Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790178 Share on other sites More sharing options...
tyra Posted March 21, 2009 Author Share Posted March 21, 2009 Jep sasa solved it. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/150435-solved-setting-comma/#findComment-790216 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.