How can CSS be utilized to style input elements, such as adding a red border, based on PHP validation errors?

To style input elements based on PHP validation errors, you can add a CSS class to the input elements when an error occurs. This class can be used to apply specific styling, such as a red border, to visually indicate the error to the user.

<?php
// PHP validation code
$errors = array();

if (/* validation condition */) {
    $errors['input_field'] = 'Error message';
}

// HTML form with input elements
?>

<form action="submit.php" method="post">
    <input type="text" name="input_field" <?php if(isset($errors['input_field'])) echo 'class="error"'; ?>>
    <?php if(isset($errors['input_field'])) echo '<span class="error-message">' . $errors['input_field'] . '</span>'; ?>
    <input type="submit" value="Submit">
</form>
```

```css
<style>
    .error {
        border: 1px solid red;
    }
    .error-message {
        color: red;
    }
</style>