How can PHP functions like fopen, fgets, and fputs be used effectively when working with text files?

When working with text files in PHP, functions like fopen, fgets, and fputs can be used effectively to read from and write to files. To read from a text file, fopen can be used to open the file, fgets can be used to read lines from the file, and fclose should be used to close the file. To write to a text file, fopen can be used to open the file in write mode, fputs can be used to write data to the file, and fclose should be used to close the file.

// Reading from a text file
$file = fopen("example.txt", "r");
while(!feof($file)) {
  $line = fgets($file);
  echo $line;
}
fclose($file);

// Writing to a text file
$file = fopen("example.txt", "w");
$data = "Hello, World!";
fputs($file, $data);
fclose($file);