title: Return in a function while in loop
body: <p>I have a function, which checks a query, and then uses a while loop to extract data, I would like to return this data but I still want it to increment, but I read that return terminates a function, and thus would make it unable to increment. Is there a way around this? </p>
  
  <pre><code>$this-&gt;messages = array();
  while($row = $data) {
                  $this-&gt;messages[$i]['id'] = $row['id'];
                  $this-&gt;messages[$i]['title'] = $row['title'];
                  $this-&gt;messages[$i]['message'] = $row['message'];
                  $i++;
  </code></pre>
  
  <p>This loop is inside the function, I want to continue until the loop is done, but then I can't return any values.. is there a work around this?</p>
  
  <p>Thank you</p>
  
  <p>EDIT:</p>
  
  <pre><code>&lt;?php
  
  function Message($username){
  
              $query = "SELECT * FROM msg WHERE `to` = '".$this-&gt;userid."' &amp;&amp; `to_viewed` = '0' &amp;&amp; `to_deleted` = '0' ORDER BY `created` DESC";   
              $stmt = $this-&gt;connection-&gt;prepare($query);
              $stmt-&gt;execute();
  
      $data = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
      $num_rows = $stmt-&gt;fetchColumn();
  
          if($num_rows) {
              $i=0;
              $this-&gt;messages = array();
              while($row = $data) {
  // want to return these 3 for each result
                  $this-&gt;messages[$i]['id'] = $row['id'];
                  $this-&gt;messages[$i]['title'] = $row['title'];
                  $this-&gt;messages[$i]['message'] = $row['message'];
  
                  $i++;
              }
          } else {
              return 1; 
          }
      }
  
  ?&gt;
  </code></pre>