What is the recommended method in PHP to create and write to a text file?

To create and write to a text file in PHP, you can use the `fopen()` function to open a file, `fwrite()` function to write to the file, and `fclose()` function to close the file. Make sure to specify the file mode as "w" to open the file for writing. You can also use the `file_put_contents()` function as a simpler alternative to write to a file in PHP.

// Using fopen(), fwrite(), and fclose()
$file = fopen("example.txt", "w");
fwrite($file, "Hello, this is a sample text.");
fclose($file);

// Using file_put_contents()
file_put_contents("example.txt", "Hello, this is a sample text.");