I think it would look cleaner to re-order the function so inside the if statement you can write directly to $out_file instead of using a variable temporarily.