title: PHP prematurely exiting while loop
body: <p>Pretty new to PHP and I am stuck on a (what I consider) a strange issue. I have spliced this file together, (it was split across several different functions) for testing, and to make it easier to explain the issue. </p>
  
  <p>This is a basic while loop in Laravel and it seems to be exiting prematurely but oddly not exiting out to the line AFTER the loop, but exiting out BEFORE the loop and then entering again. Can't for the life of me workout why. I have added some log events throughout the function so I could try understand what was happening. </p>
  
  <p>This is correctly fetching and writting products to the DB up until page 7 and then I get a "Start API Helper" event on log but never get an "End API helper". So somewhere in page 7 something is causing the while loop to exit out to the lines above, reset the pagecount to 0. then re-enter the loop, fetching the first lot of products and throwing an SQL duplicate key exception when writing. I know it is exiting out to before the loop as I get a "Before while", right after "New Client" in the log. And of course, the pagecount is resetting. How can this happen? </p>
  
  <p>Any help would be so greatly appreciated.</p>
  
  <pre><code>public function store()
  {
      $items = array();
      Log::info('Before while');
      $pagecount = 0;
      $prodcount = 1;
       while ($prodcount &gt; 0) {
          Log::info('Top of while');
          $prodcount = 0; // Reset product counter
          Log::info('Page Count:'.$pagecount);
          Log::info('Start API Helper');
      $headers = array(
          'NETOAPI_KEY' =&gt; env('NETO_API_KEY'),
          'Content-Type' =&gt; 'application/json',
          'Accept' =&gt; 'application/json',
          'NETOAPI_ACTION' =&gt; 'GetItem'
      );
      Log::info('Headers Declared');
      $body = '{
          "Filter": {
              "DateAddedFrom" : "2013-06-01 12:00:00",
              "Page": "'.$pagecount.'",
              "Limit": "500",
              "OutputSelector": [
                  **Lots of JSON here - removed for readability**
              ]
          }
      }'; 
  
      Log::info('Start API Helper');
      $client = new client();
      Log::info('New Client');
      try {
         $res = $client-&gt;post(env('NETO_API_URL'), [ 'headers' =&gt; $headers , 'body' =&gt; $body ]);
      } catch (RequestException $e) {
          echo $e-&gt;getRequest();
          Log::error($e-&gt;getRequest());
          if ($e-&gt;hasResponse()) {
              echo $e-&gt;getResponse();
              Log::error($e-&gt;getResponse());
          }
      }
      Log::info('End API Helper');
      $items = json_decode(utf8_encode($res-&gt;getBody()), true);
          foreach($items["Item"] as $item)
          {
              // JSON is returning an empty array for empty strings. Need to convert to empty string for SQL save.
              foreach($item as $key =&gt; $value){
                  if (empty($value)) {
                       $item["$key"] = "";
                  }
              }
              $Product = new Item;
              $Product-&gt;SKU = array_get($item, 'SKU');
              ** LOTS OF DB FIELDS REMOVED FROM HERE FOR READABILITY**
              $Product-&gt;save();
              $prodcount++;               
          } 
          $pagecount++;
          Log::info($prodcount.' products written to DB:');
          Log::info('Bottom of while'); 
      };
      Log::info('Exited While'); 
      return 'Complete';
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> I have modified the ELoquent SQL Client to a firstorNew to avoid any potential Duplicate key errors. Now the logs show the same 'while exit' at page 7 (occasionally page 8) which then re-enters the loop at page 0 BUT the weirdest thing is that it seems to end up with 2 instances of the loop running. Then eventually 3 instances, then 4. Then it throws a memory exhausted exception. The logs looks like.</p>
  
  <pre><code>local.INFO: Page Count:1
  local.INFO: Page Count:2
  local.INFO: Page Count:3
  local.INFO: Page Count:4 
  local.INFO: Page Count:5 
  local.INFO: Page Count:6 
  local.INFO: Page Count:7 
  local.INFO: Page Count:8
  local.INFO: Page Count:0
  local.INFO: Page Count:9
  local.INFO: Page Count:1
  local.INFO: Page Count:10 
  local.INFO: Page Count:2
  local.INFO: Page Count:11 
  local.INFO: Page Count:3
  local.INFO: Page Count:12 
  </code></pre>
  
  <p>...And so on...</p>
  
  <p>Is it possible that a malformed JSON API response could be causing this? Perhaps I'll try limit the paging to 25 or so, so I can access the response causing the loop break?</p>