title: php variable scope inside a loop in a function
body: <p>I am wondering is there any way to get the value of a variable, that was defined in a loop inside a function?</p>
  
  <p>Do variables declared in a loop / if-then-else, etc have a limited scope (only inside these blocks)?</p>
  
  <p>here is an example of a function:</p>
  
  <pre><code>&lt;?php
  
  function getComments($tabName) {
      $sql = "select
                  a.owner,
          a.table_name,
          a.column_name,
          a.data_type,
          a.data_length,
          a.data_precision,
          b.comments
              from 
                  all_tab_columns a,
          user_col_comments b
              where 
                  a.TABLE_NAME = b.table_name
          and a.COLUMN_NAME = b.column_name
          and a.owner = 'CORE' 
          and a.table_name ='" . $tabName . "'
              order by 
                  a.column_id";
      $stid = oci_parse(getConnect(), $sql);
  
      // runs the query above                   
      oci_execute($stid);
      while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
          foreach ($row as $column =&gt; $entry) {
              // Column name
              if ($column == 'COLUMN_NAME') {
                  $output = "this is a column";
              }
          }
      }
  
      return $output;
  }
  </code></pre>
  
  <p>And here I get the "Undefined variable: output" error.</p>
  
  <p>if I put echo instead the <code>$output</code> variable, I get the result.</p>
  
  <p>Is it because the <code>$output</code> scope? How can I get the <code>$output</code> value in my return?</p>