What are best practices for using conditional statements in PHP to change the color of HTML elements based on variable values?

When using conditional statements in PHP to change the color of HTML elements based on variable values, it is important to use the appropriate syntax and logic to ensure the desired outcome. One common approach is to use an if-else statement to check the value of the variable and then dynamically set the color attribute of the HTML element based on the condition.

<?php
$variable = 5;

if ($variable < 5) {
    $color = 'red';
} elseif ($variable >= 5 && $variable < 10) {
    $color = 'green';
} else {
    $color = 'blue';
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Conditional Color Change</title>
</head>
<body>
    <div style="color: <?php echo $color; ?>">This text changes color based on the variable value.</div>
</body>
</html>