If you're writing new code, just use the filesystem functions and write directly into files.
If you're dealing with a significant amount of legacy code that has echo and print statements scattered throughout, use the output buffering functions. Turn on buffering, include the old code, then write the buffered output to an appropriate file. Look up ob_start in the manual.