title: How to access multiple variables outside foreach loop in php
body: <p>I am not a php pro and have already explored different online search forums but could not find any suitable answer and that's why posting my question here.</p>
  <p><strong>Function One: Runs a query to database and define variables in foreach loop as follows:</strong></p>
  <pre><code>function myFunction(){
   $dbinfo = get_db_info();
   $users = $dbinfo-&gt;get_results(&quot;
      SELECT 
         Email,
         ID,
         Name,
         Address
          FROM Database_table
          WHERE ID = 5
      &quot;);
      
      if($users) {
          $output = '';
          foreach($users as $user) {
              $ID = $user-&gt;ID;
              $dname = $user-&gt;Name;
              $email = $user-&gt;Email;
              $address = $user-&gt;Address;
          }else{
              $output .= 'No user found';
          }
  
      }//endif
      return $output;
  }
  </code></pre>
  <p><strong>Function two: Now I want to get the values of those variables in another function as follows:</strong></p>
  <pre><code>function dislayData(){
  
      $values = myFunction();
      echo $values=&gt;$email;
  
  }
  </code></pre>
  <p>But it does not work. It seems I may need to wrap variables in the foreach loop into an array. I don't understand how to do that.</p>
  <p>Any help here would be highly appreciated.</p>
  <p>Thanks in advance.</p>
  <p>Bakar</p>