Are there best practices for structuring PHP code within HTML elements for dynamic styling?

When structuring PHP code within HTML elements for dynamic styling, it is best practice to separate PHP logic from HTML markup to maintain clean and readable code. One way to achieve this is by using PHP to set variables that control the styling of HTML elements, then applying those variables within the HTML code.

<?php
// PHP logic to determine styling
$color = "red";
$font_size = "20px";
?>

<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Styling with PHP</title>
    <style>
        .dynamic-element {
            color: <?php echo $color; ?>;
            font-size: <?php echo $font_size; ?>;
        }
    </style>
</head>
<body>
    <div class="dynamic-element">
        This text will be styled dynamically using PHP.
    </div>
</body>
</html>