The fwrite function is used to write data to a text file. This function has two parameters. The first parameter is the file handle and second one is the string of data to be written.
To be written, the file must be opened first using the fopen function.
fopen(filename, mode)
The mode parameter specifies the type of access you require to the stream. In this case we use ‘w’ for writing only. This function returns filehandle.
$textFile = “textFile.txt”;
$filehandle = fopen($textFile, ‘w’) or die(”can’t open file”);
$stringData = “First line string\n”;
fwrite($filehandle, $stringData);
$stringData = “Second line string\n”;
fwrite($filehandle, $stringData);
fclose($filehandle);
As you can see, the fclose function closes the text file.