What is the recommended way to implement a color change on a PHP form for error messages?

To implement a color change on a PHP form for error messages, you can use CSS to style the error messages with a different color. You can add a class to the error message div and define the color in your CSS file. This way, when an error message is displayed, it will automatically have the specified color.

<?php
// Check for form submission and validate input
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    
    // Validate name
    if (empty($name)) {
        $error = "Name is required.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .error {
            color: red;
        }
    </style>
</head>
<body>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
        <input type="text" name="name" placeholder="Name">
        <?php if (isset($error)) { ?>
            <div class="error"><?php echo $error; ?></div>
        <?php } ?>
        <button type="submit">Submit</button>
    </form>
</body>
</html>