What are best practices for handling LDAP connection errors in PHP?

When handling LDAP connection errors in PHP, it is important to implement error handling to gracefully manage any potential issues that may arise during the connection process. This can include checking for connection errors, handling exceptions, and providing appropriate error messages to the user.

<?php
$ldapconn = ldap_connect("ldap.example.com");

if (!$ldapconn) {
    die("Could not connect to LDAP server");
}

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);

$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");

if (!$ldapbind) {
    die("LDAP bind failed");
}

// LDAP operations go here

ldap_close($ldapconn);
?>