How can PHP be used to read data from a text file and format it with specific colors for each line?

To read data from a text file and format it with specific colors for each line in PHP, you can use the `file()` function to read the contents of the file into an array, and then loop through each line to apply the desired color formatting using HTML `<span>` tags. You can use a counter variable to alternate between different colors for each line.

$file = &#039;data.txt&#039;;
$lines = file($file);

$counter = 0;
foreach ($lines as $line) {
    if ($counter % 2 == 0) {
        echo &#039;&lt;span style=&quot;color: red;&quot;&gt;&#039; . $line . &#039;&lt;/span&gt;&lt;br&gt;&#039;;
    } else {
        echo &#039;&lt;span style=&quot;color: blue;&quot;&gt;&#039; . $line . &#039;&lt;/span&gt;&lt;br&gt;&#039;;
    }
    $counter++;
}