title: PHP function doesn't return value
body: <p>I have a function which, given a filename and directory path, checks if the directory already contains a file with the same name and if so returns an amended filename (by appending a number after the first part of the filename).
  (The get_filenames() function is a CodeIgniter helper function which creates an array of all the filenames in the specified directory.)</p>
  
  <p>When I try to print out the returned result of the function call, I get nothing; but if I print $new_filename in the else{} statement of the function itself, then simply call the function (rather than printing it's value), it works! </p>
  
  <p>I need to return the value in the function, not print it, as I actually need to assign the result to a variable for further processing. (In the example below I've just printed the result of the function call to demonstrate the point.)</p>
  
  <p>The function:</p>
  
  <pre><code>function avoid_conflicting_filenames($old_filename, $new_filename, $dir, $count)
  {   
      $num = '';
      if ($count &gt; 0):
          $num = $count;
      endif;
  
      $filename_arr = explode('.', $old_filename, -1);
      $new_filename = $filename_arr[0] . $num . '.' . $filename_arr[1];
  
      if (in_array($new_filename, get_filenames($dir))):      
          $count++;
          avoid_conflicting_filenames($old_filename, $new_filename, $dir, $count);
      else:
          return $new_filename;
      endif;
  }
  </code></pre>
  
  <p>And where I call the function:</p>
  
  <pre><code>print avoid_conflicting_filenames('file.jpg', '', 'path/to/file', 0);
  </code></pre>
  
  <p>This has been driving me insane for the past day, so any help would be greatly appreciated! Thanks.</p>