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
Share on other sites

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.

Link to comment
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?

Link to comment
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?

 

he was replying to my post above.

Link to comment
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.

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.