Hi all, I am trying to get a for loop to cycle through, sleep, and then keep going until a specified time. I'm running into memory errors and unsure whether this is legitimate, or because its created a memory leak. My code is:

$currentTime = time();
$time = time();

while ($currentTime < $time + 1800)
{

    for($i = 0; $i < 180; $i++)
    {
        //code

        if($i == 180 -1):
        $i = 0;
        sleep(600);
        endif;

        $currentTime = time();
    }
}

My logic is that when the for loop gets to the end, it resets it self for the next set, and when it wakes up again it can start from the beginning. It will do this until the current time equal approaches the $time + 1800 value.... Is this the correct approach? It's a hard issue to diagnose since the error is simply a memory one.

Member Avatar for diafol

Sleeping on the server doesn't make much sense, does it? WHat's this?

if($i == 180 -1):

I think you've got an issue with time(). Can't see what on earth you're doing with this code. Setting stuff to time() could be a different value every time you set it.

< 180is the limit on the for loop. 180 - 1is the value of $i on the last loop.

The purpose of setting time() is to have a different values, and when while ($currentTime < $time + 1800) i.e., when the time right now is equal to or greater than the time we started the loop + 1800 seconds, the loop stops.

Anyway, I have found a solution that doesn't use a while loop. Instead I reset my $i value and use a variable $iteration to track how many times the for loop has executed. It works very well!

for($i = 0; $i < $loops; $i++)
{
    //Code

    if($i == $loops - 1): //IF $i is last in loop
    $i = -1; //Will change to zero at end of this loop
    $iteration++;
    sleep($x_seconds)
    elseif($iteration == 10): //10 = final iteration
        break;
    endif;

}

This loops through the values of $i, waits for $x_seconds, then continues for another iteration until equal to the iteration variable.

Your solution is NOT a solution but a badly patch job that is bloated and makes the script worse.

Your for loop has NOTHING to do with $loops value because you simply want your $iteration to be at most 10. If you really want to use for loop, simply replace $loops with $iteration and get rid of your $iteration inside the loop.

Anyway, your original script is already wrong. When you try to loop through something, you should make it as simple as it could. Also, THINK about how a loop is going. In your original script, you DO NOT NEED another for loop inside the while loop at all. You are making it more complicated and gain nothing but make it worse.

You seem to be a self-taught person that does not learn from a good source. The idea of how to iterate through a time is very simple, you need 2 varaibles -- one is dynamic and the other is fixed. When you iterate through a loop, the dynamic keeps changing but the fixed must stay the same. Your first instinct is correct to use while loop. What you need is the correct the $time to be fixed and NOT use it in the while loop. It should be $time = time()+1800; or $time = $currentTime+1800; instead. Then in your while loop, it would be while ($currentTime < $time) { ... }.

Below is a modified script. I added $sleepTime just to make it more flexible. So your whole script could be very short as...

$currentTime = time();
$time = $currentTime+1800;
$sleepTime = 600;
while ($currentTime<$time) {
  // do whatever you want???
  sleep($sleepTime);
  $currentTime += sleepTime;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.