What best practices should be followed when handling error messages in PHP sessions for user input validation?

When handling error messages in PHP sessions for user input validation, it is important to provide clear and specific error messages to the user to help them understand what went wrong. It is also crucial to store these error messages in session variables so they can be displayed on the next page load. Finally, make sure to unset the error messages from the session once they have been displayed to the user to avoid confusion.

// Set error message in session variable
$_SESSION['error_message'] = "Please enter a valid email address.";

// Display error message on the next page load
if(isset($_SESSION['error_message'])) {
    echo $_SESSION['error_message'];
    unset($_SESSION['error_message']); // Unset the error message
}