What are some common mistakes to avoid when checking for IP address validity in PHP?

One common mistake to avoid when checking for IP address validity in PHP is not using the filter_var function with the FILTER_VALIDATE_IP filter. This function will ensure that the IP address is in a valid format. Additionally, it's important to handle both IPv4 and IPv6 addresses properly to avoid any errors.

// Check for valid IPv4 address
$ip = '192.168.1.1';
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    echo 'Valid IPv4 address';
} else {
    echo 'Invalid IPv4 address';
}

// Check for valid IPv6 address
$ip = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    echo 'Valid IPv6 address';
} else {
    echo 'Invalid IPv6 address';
}