What are some best practices for handling file operations in PHP, such as reading and writing text files?

When handling file operations in PHP, it is important to ensure proper error handling, use appropriate file modes, and close file handles after use to prevent memory leaks. Reading and writing text files can be done using functions like fopen, fread, fwrite, and fclose.

// Example of reading a text file in PHP
$filename = "example.txt";
$file = fopen($filename, "r") or die("Unable to open file!");

while (!feof($file)) {
    echo fgets($file) . "<br>";
}

fclose($file);
```

```php
// Example of writing to a text file in PHP
$filename = "example.txt";
$file = fopen($filename, "w") or die("Unable to open file!");

$text = "Hello, world!";
fwrite($file, $text);

fclose($file);