taps Posted February 2, 2007 Share Posted February 2, 2007 <?php $i = 1; while ($i <= 10) { echo $i++; //[b]------This works. [/b] } $filename = 'text.txt'; $fsize = '800'; print "<br>$filename"; $a='0'; while ($a < $fsize){ $file=fopen($filename,'a'); fwrite($file, "e"); //open the file fclose($file); $a = filesize($filename); echo "<br>$a<br>"; // [b]It's works only one time.[/b] } ?> Why does it works still ? Why doesn't 'while' stops when $a < $fsize ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 2, 2007 Share Posted February 2, 2007 What does it echo for $a? If it's 0, then your file isn't being written correctly. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 maybe it's because your int values are actually strings...? Quote Link to comment Share on other sites More sharing options...
taps Posted February 2, 2007 Author Share Posted February 2, 2007 echo for $a it's filesize($filename). If it's 0, then your file isn't being written correctly Why not ? Look at the code please. $a=0; before loop inside loop : $a= filesize($filename); Run the code and you will see what is wrong. Quote Link to comment Share on other sites More sharing options...
taps Posted February 2, 2007 Author Share Posted February 2, 2007 maybe it's because your int values are actually strings...? yeah but : $total = "100 beer"; $incoming = 5; $total = $incoming + $total;// $total= 105 Quote Link to comment Share on other sites More sharing options...
.josh Posted February 2, 2007 Share Posted February 2, 2007 based on the code you provided, it executes the first time because initially, $a is set to 0 and $fsize is set to 800. Then at the end of your loop, you set $a to the filesize of your file. Your filesize must be > than 800, therefore, the loop ends. as far as this: $total = "100 beer"; $incoming = 5; $total = $incoming + $total;// $total= 105 I don't see how that relates to your original code, seeing as how it's not included in your original code, but at face value, when you add a string and an integer together like that, it will indeed produce your 105, but you will lose the "beer" part of your string. Is that what you want? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 2, 2007 Share Posted February 2, 2007 based on the code you provided, it executes the first time because initially, $a is set to 0 and $fsize is set to 800. Then at the end of your loop, you set $a to the filesize of your file. Your filesize must be > than 800, therefore, the loop ends. as far as this: $total = "100 beer"; $incoming = 5; $total = $incoming + $total;// $total= 105 I don't see how that relates to your original code, seeing as how it's not included in your original code, but at face value, when you add a string and an integer together like that, it will indeed produce your 105, but you will lose the "beer" part of your string. Is that what you want? he was replying to my post above. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 2, 2007 Share Posted February 2, 2007 Seems to work on mine. I created the text file, ran the code and it increments by 1 digit each time I refresh the page. Quote Link to comment Share on other sites More sharing options...
taps Posted February 2, 2007 Author Share Posted February 2, 2007 based on the code you provided, it executes the first time because initially, $a is set to 0 and $fsize is set to 800. Then at the end of your loop, you set $a to the filesize of your file. Your filesize must be > than 800, therefore, the loop ends. Yes should be but it works still. It still show to me 1 and never ends. Sorry I'm not a programmer, I play with php. I need easy script with 'while' which will make for me file for example 1MB. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 2, 2007 Share Posted February 2, 2007 That's not what your comments in the code said. You're now saying it prints endless '1's. Did you see this note in the filesize function? Note: The results of this function are cached. See clearstatcache() for more details. Perhaps that will help. Quote Link to comment Share on other sites More sharing options...
taps Posted February 2, 2007 Author Share Posted February 2, 2007 thanks, everybody and jesirose It's works <?php $filename = 'text.txt'; $fsize = '800'; print "<br>$filename"; $a='0'; while ($a < $fsize){ $file=fopen($filename,'a'); clearstatcache(); // I needed this:)))) fwrite($file, "e"); fclose($file); $a = filesize($filename); echo "<br>$a<br>"; } ?> Quote Link to comment 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.