The
file_put_contents() filesystem function in PHP will write string data to a file. It works the same as if you were to use fopen(), fwrite(), and fclose() together to write data to a file. The file will be created if the output file does not exist yet, and if it already exists it will be overwritten.

<?php
$file = "myfile.txt";
$myString = "Goodbye World";
file_put_contents($file, $myString);
$getData = file_get_contents($file);
echo $getData;
?>
Goodbye World
If you would like to append to existing data in a file that already exists you can use the third parameter for this function.
<?php
$file = "myfile.txt";
$myString = ", I am going home.";
file_put_contents($file, $myString, FILE_APPEND);
$getData = file_get_contents($file);
echo $getData;
?>
Goodbye World, I am going home.