What PHP functions or methods can be used to convert and compare IPv4 and IPv6 addresses effectively for database storage and retrieval purposes?

When dealing with storing and retrieving IPv4 and IPv6 addresses in a database, it is important to ensure that the addresses are properly converted and compared. One way to achieve this is by using PHP's `inet_pton()` function to convert the IP address to a binary format for storage, and `inet_ntop()` function to convert it back to a human-readable format for retrieval. Additionally, you can use the `inet_pton()` function to compare two IP addresses effectively.

// Convert IPv4 or IPv6 address to binary format for storage
$ipAddress = '192.168.1.1';
$binaryIpAddress = inet_pton($ipAddress);

// Convert binary format back to human-readable format for retrieval
$convertedIpAddress = inet_ntop($binaryIpAddress);

// Compare two IP addresses effectively
$ipAddress1 = '192.168.1.1';
$ipAddress2 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
$binaryIpAddress1 = inet_pton($ipAddress1);
$binaryIpAddress2 = inet_pton($ipAddress2);

if ($binaryIpAddress1 == $binaryIpAddress2) {
    echo 'IP addresses are equal';
} else {
    echo 'IP addresses are not equal';
}