title: PHP - possible hurdles when executing long script
body: <p>I have a quite long loop running in a function , but does not finish all the eventual iterations and stops without giving any errors  :</p>
  <pre><code>function my_function(){
  
      foreach (range(100,999) as $art_id){ 
          
      
      $current++;//see bottom flush functions...
      outputProgress($current, $art_id );//see bottom flush functions...
  
  // do a lot of stuff on remote URL...
      // including download images , 
      // scraping HTMl etc ..
      
      }
  }
  </code></pre>
  <p>I am using some output progress with flush to track the progress</p>
  <pre><code>function outputProgress($current, $total) {
      // echo &quot;&lt;span style='background:red;font-size:1.5em;color:#fff;'&gt;&quot; . round($current / $total * 100) . &quot;% &lt;/span&gt;&quot;;
      echo &quot;&lt;span style='background:red;font-size:1.5em;color:#fff;'&gt;&quot; . $current .'/'. $total  . &quot;% &lt;/span&gt;&quot;;
      myFlush();
      sleep(1);
  }
  </code></pre>
  <p>and</p>
  <pre><code>function myFlush() {
      echo(str_repeat(' ', 256));
      if (@ob_get_contents()) {
          @ob_end_flush();
      }
      flush();
  }
  </code></pre>
  <p>(never mind the percentage calculation , it is now disabled and just shows the ID of iteration)</p>
  <p>I have noticed that most of the time I am executing the loop,
  It will just STOP after 20-25 iterations . sometimes as little as 10.</p>
  <p>My first suspects were the <code>time limit</code> , and <code>max_execution time</code>, so I added  :</p>
  <pre><code>set_time_limit(99999);
  ini_set('max_execution_time', 99999);
  
  function my_function(){
  
      foreach (range(410,499) as $art_id){ // done 500-600
  
      set_time_limit(99999);
      ini_set('max_execution_time', 99999);
      
      // do a lot of stuff on remote URL...
      // including download images , 
      // scraping HTMl etc ..
      }
  }
  </code></pre>
  <p>As you can see, I have added those both <code>INSIDE</code> and <code>OUTSIDE</code> the function itself, just in case .</p>
  <p>But it does not help much , and the loop still stops .
  My next suspect was the <code>Memory limit</code>, so I added :</p>
  <pre><code>ini_set('memory_limit','128M');
  </code></pre>
  <p>and since I am working on wp, I also tried</p>
  <pre><code>define('WP_MEMORY_LIMIT', '128M'); 
  </code></pre>
  <p>but to no avail. The scipt still stops after little iterations .</p>
  <ul>
  <li><strong>What are the other  possible causes for this behavior , and the
  possible remedies ?</strong></li>
  </ul>
  <p>Mind you - the script does not give any errors, it just stops at a certain loop.</p>
  <p><strong>EDIT I</strong></p>
  <p>I have pasted the script <a href="http://pastebin.com/nSQhVx1h" rel="nofollow noreferrer">HERE</a>
  . it is actually a slightly modified scrap_slashdot() function from the <a href="http://simplehtmldom.sourceforge.net/" rel="nofollow noreferrer">simplehtmldom</a> lib included examples.</p>
  <p>It is modified to insert wordpress posts while also downloading images and attaching them.</p>
  <p><strong>EDIT II</strong>
  Using @Allendar comment <code>echo ini_get('memory_limit');</code> seems like it works and it is set to 128M..</p>