<?php ob_start(); echo '<?php'; // this will go into our file // stop php from interpreting, now write content of file freely! ?> echo 'hello, world!'; <?php echo '?>'; // this will go into our file $content = ob_get_flush(); file_put_contents('foo.txt', $content); ?>