How can CSS be utilized in conjunction with PHP to format and display chat messages in a desired layout, such as aligning messages to the left or right?

To format and display chat messages in a desired layout using CSS and PHP, you can assign specific classes to messages based on whether they are sent by the current user or the other participants. By using CSS to style these classes with properties like text-align and float, you can align messages to the left or right accordingly.

<?php
// Assuming $message is the message content and $isCurrentUser is a boolean indicating if the message is from the current user

if ($isCurrentUser) {
    echo '<div class="message current-user">' . $message . '</div>';
} else {
    echo '<div class="message other-user">' . $message . '</div>';
}
?>
```

CSS:
```css
.message {
    padding: 5px;
    margin: 5px;
    border-radius: 5px;
}

.current-user {
    text-align: right;
    background-color: #DCF8C6;
}

.other-user {
    text-align: left;
    background-color: #E6E6E6;
}