How can the use of $_SESSION['success'] and $_SESSION['error'] variables be optimized in PHP scripts to avoid conflicts?

When using $_SESSION['success'] and $_SESSION['error'] variables in PHP scripts, conflicts can arise if both variables are used in the same script. To avoid conflicts, it is recommended to namespace the session variables by prefixing them with a unique identifier. This way, each type of message (success or error) can be stored separately without interfering with each other.

// Set success message
$_SESSION['myapp_success'] = "Success message here";

// Set error message
$_SESSION['myapp_error'] = "Error message here";

// Retrieve and display success message
if(isset($_SESSION['myapp_success'])){
    echo $_SESSION['myapp_success'];
    unset($_SESSION['myapp_success']);
}

// Retrieve and display error message
if(isset($_SESSION['myapp_error'])){
    echo $_SESSION['myapp_error'];
    unset($_SESSION['myapp_error']);
}