The following code writes to a file, it will overwrite the current contents of the file:
$string = "file contents goes here";
$myFile = "file.php";
$fh = fopen($myFile, 'w') or die("can't open file");
Make sure you give file.php write access on the server (Apache chmod 775 normally works).