How should one properly compare IP addresses in PHP to avoid errors and ensure accurate results?

When comparing IP addresses in PHP, it is important to use the `inet_pton` function to convert the IP addresses to binary format before comparing them. This ensures that the comparison is done accurately and without errors, as comparing IP addresses as strings may lead to unexpected results due to different formats or representations.

$ip1 = '192.168.1.1';
$ip2 = '192.168.1.1';

$binaryIp1 = inet_pton($ip1);
$binaryIp2 = inet_pton($ip2);

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