How can one troubleshoot and debug LDAP or LDAPS connection issues in PHP effectively?

To troubleshoot and debug LDAP or LDAPS connection issues in PHP effectively, you can start by checking the LDAP server settings, ensuring the correct port is used, verifying the SSL certificate if connecting over LDAPS, and checking for any firewall restrictions. Additionally, enabling error reporting and logging can help identify any specific errors or issues with the connection.

<?php
$ldap_server = 'ldaps://ldap.example.com';
$ldap_port = 636;

$ldap_conn = ldap_connect($ldap_server, $ldap_port);

if (!$ldap_conn) {
    echo "Failed to connect to LDAP server";
    exit;
} else {
    echo "Successfully connected to LDAP server";
}

ldap_close($ldap_conn);
?>