How can one effectively handle errors in PHP, especially when using functions like ldap_bind that throw notices?

When handling errors in PHP, especially when using functions like ldap_bind that throw notices, it is important to use error handling techniques such as try-catch blocks to catch and handle any exceptions that may arise. This allows you to gracefully handle errors and prevent them from interrupting the flow of your program.

try {
    // LDAP connection code
    $ldapconn = ldap_connect("ldap.example.com");
    if (!$ldapconn) {
        throw new Exception("Unable to connect to LDAP server");
    }

    // LDAP bind code
    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    if (!$ldapbind) {
        throw new Exception("LDAP bind failed");
    }

    // Other LDAP operations

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}