PHP already has a built-in function to do this, file_put_contents(). The syntax is:
file_put_contents($filename, $data, FILE_APPEND);
Note that file_put_contents()
will create the file if it does not already exist (as long as you have file system permissions to do so).