What are the best practices for implementing conditional formatting in PHP to display data in different colors?
To implement conditional formatting in PHP to display data in different colors, you can use if statements to check the conditions and apply appropriate styling based on the data values. This can be done by using inline CSS or by adding CSS classes dynamically to the HTML elements.
<?php
// Sample data array
$data = [10, 20, 30, 40, 50];
// Loop through the data and apply conditional formatting
foreach ($data as $value) {
if ($value < 20) {
echo '<span style="color: red;">' . $value . '</span><br>';
} elseif ($value >= 20 && $value < 40) {
echo '<span style="color: blue;">' . $value . '</span><br>';
} else {
echo '<span style="color: green;">' . $value . '</span><br>';
}
}
?>