What are the best practices for organizing and displaying messages based on user interactions in PHP?

When organizing and displaying messages based on user interactions in PHP, it is important to use a consistent and clear system to communicate information effectively. One common practice is to use session variables to store messages that can be displayed on subsequent pages. By setting and displaying messages in this way, you can ensure that users receive relevant feedback without cluttering the interface.

// Set message in session
$_SESSION['message'] = "Success! Your changes have been saved.";

// Display message on the page
if(isset($_SESSION['message'])) {
    echo '<div class="alert alert-success">' . $_SESSION['message'] . '</div>';
    unset($_SESSION['message']);
}