How can CSS classes be used to dynamically style elements based on their content in PHP-generated HTML?

To dynamically style elements based on their content in PHP-generated HTML, you can use CSS classes in conjunction with PHP logic to apply different styles based on conditions. You can add a class to the HTML element based on the content generated by PHP, and then define the styles for each class in your CSS.

<?php
// Sample PHP code generating HTML with dynamic styling based on content
$content = "Lorem ipsum dolor sit amet";
$styleClass = '';

if(strlen($content) < 20) {
    $styleClass = 'short-text';
} else {
    $styleClass = 'long-text';
}

echo '<div class="' . $styleClass . '">' . $content . '</div>';
?>

<style>
.short-text {
    color: red;
}

.long-text {
    color: blue;
}
</style>