Jump to content

Why doesn't 'while' works ?


taps

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/36783-why-doesnt-while-works/
Share on other sites

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?

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.

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.

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.

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>";
} 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.