How can we write data to a text file in PHP?
To create a text file and write data in PHP, you can utilize the built-in function file_put_contents().
Here is an example code that demonstrates how to use the file_put_contents() function to create a text file named “example.txt” and write “Hello, World!” into it.
<?php
$file = 'example.txt';
$data = 'Hello, World!';
// 将数据写入文本文件
if (file_put_contents($file, $data) !== false) {
echo "数据写入成功!";
} else {
echo "数据写入失败!";
}
?>
After running the above code, if no errors occur, you will create a text file named “example.txt” in the current directory, which will contain the content “Hello, World!”.