What are some potential pitfalls when trying to edit a specific line in a text file using PHP?

When trying to edit a specific line in a text file using PHP, one potential pitfall is that the file may be too large to read into memory all at once. To solve this, you can read the file line by line and store the contents in an array, then modify the specific line in the array before writing it back to the file.

$file = 'example.txt';
$line_number = 3; // line number to edit
$new_content = "New content for line 3";

$lines = file($file, FILE_IGNORE_NEW_LINES);
$lines[$line_number - 1] = $new_content;

file_put_contents($file, implode("\n", $lines));