Jump to content

[SOLVED] setting comma


tyra

Recommended Posts

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.