Using $_SESSION to Display Errors

if ( !session_id() ) {
    session_start();
}
if ($error) {
    $_SESSION['my_plugin_errors'] = $error->get_error_message();
}
if ( array_key_exists( 'my_plugin_errors', $_SESSION ) ) {?>
    <div class="error">
        <p><?php echo $_SESSION['my_plugin_errors']; ?></p>
    </div><?php

    unset( $_SESSION['my_plugin_errors'] );
}