title: Return content from function instead of echo
body: <p>I have this recursive function:</p>
  
  <pre><code>    function displayTreeview($cats,$depth=0)
      {
          //if($depth==0){echo '&lt;ul&gt;';}
          foreach($cats as $cat)
          {
              $hasChildren=count($cat['children'])&gt;0;
              $class = $hasChildren?' class="menu-item-has-children"':'';
              echo '&lt;li'.$class.' data-children="'.count($cat['children']).'"&gt;'.$cat['name'];
              if($hasChildren){
               echo '&lt;ul class="dropdown-menu sub-menu"&gt;';
               displayTreeview($cat['children'],$depth+1);echo '&lt;/ul&gt;';}
               echo '&lt;/li&gt;';
          }
          //if($depth==0){echo '&lt;/ul&gt;';}
      }
  </code></pre>
  
  <p>How can I get all html code into a var and return it? I  tried with $var .= to append but did not succeded to get the correct code.</p>