How can one remove a specific line from a text file using PHP?

To remove a specific line from a text file using PHP, you can read the contents of the file, remove the desired line, and then rewrite the modified contents back to the file. One way to achieve this is by reading the file line by line, skipping the line you want to remove, and then writing the remaining lines back to the file.

<?php
$filename = 'example.txt';
$line_number_to_remove = 3;

$lines = file($filename, FILE_IGNORE_NEW_LINES);
unset($lines[$line_number_to_remove - 1]);

file_put_contents($filename, implode("\n", $lines));
?>