What are some common methods for reading and modifying files in PHP?

One common method for reading and modifying files in PHP is using the file_get_contents() function to read the contents of a file into a string variable, and then using file_put_contents() function to write new content back to the file. Another approach is to use fopen() function to open a file, fread() function to read its contents, and fwrite() function to modify and write back to the file.

// Reading a file using file_get_contents()
$fileContent = file_get_contents('example.txt');
echo $fileContent;

// Modifying and writing to a file using file_put_contents()
$newContent = "This is the new content";
file_put_contents('example.txt', $newContent);

// Reading a file using fopen(), fread(), and fwrite()
$file = fopen('example.txt', 'r+');
$content = fread($file, filesize('example.txt'));
$newContent = "This is the new content";
fwrite($file, $newContent);
fclose($file);