Skip to content
PHP Logo

How can CSS classes be effectively used to format specific PHP variables within HTML elements?

To format specific PHP variables within HTML elements using CSS classes, you can assign unique class names to the HTML elements that will contain the PHP variables. Then, define corresponding CSS rules for those classes to style the variables as desired. This allows for easy customization and separation of styling from content in your code.

php
<?php
$variable1 = "Hello";
$variable2 = "World";
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .php-variable {
            color: blue;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <p>Example using CSS classes to format PHP variables: <span class="php-variable"><?php echo $variable1; ?></span> <span class="php-variable"><?php echo $variable2; ?></span></p>
</body>
</html>