title: In a PHP recursive function, how can I check it's the last iteration?
body: <p>I have this recursive function up and running:</p>
  
  <pre><code>function my_function($a, $b, $level=0){
  
  
      $items = get_some_items($a, $b);
  
      $fh = fopen($fpath, 'a+');
  
      foreach($items as $item){
          fwrite($fh, "Some info related to $item");
  
          if( /* $item has something special */ ){
  
              my_function($a, $item, $level++);
          }
      }
  
  
      if( /* we a re at the last recursion */ ){
          //do something extra special;
          //e.g. fwrite($fh, "This is the end of the file");
      }
  
  
      fclose($fh);
  
  }
  </code></pre>
  
  <p>With this, I can tell which iteration is the first run. I can also get the <strong>nth</strong> run at any level.</p>
  
  <p>My question: I'd like to do something special during the very last run. Is there even a way to neatly achieve this?</p>