Are there any best practices for using boolean flags to handle errors in PHP scripts?

When using boolean flags to handle errors in PHP scripts, it is important to set the flag to true when an error occurs and check the flag before proceeding with any further actions. This can help in easily identifying and handling errors in the script.

$error = false;

// Code that may produce an error
if ($error_condition) {
    $error = true;
}

// Check the error flag before proceeding
if ($error) {
    // Handle the error
    echo "An error occurred.";
} else {
    // Proceed with the rest of the script
}