How can PHP be used to compare input data with existing data in a text file?

To compare input data with existing data in a text file using PHP, you can read the contents of the text file into an array and then loop through the array to compare each line with the input data. You can use functions like file_get_contents() to read the file and explode() to split the contents into an array. Then, iterate through the array to compare each line with the input data.

<?php
// Input data
$input_data = "example data";

// Read contents of the text file into an array
$file_contents = file_get_contents("data.txt");
$data_array = explode("\n", $file_contents);

// Compare input data with existing data
foreach ($data_array as $line) {
    if ($line == $input_data) {
        echo "Input data matches existing data: " . $line;
        break;
    }
}
?>