What are the implications of not properly validating and formatting IP addresses in PHP?

Not properly validating and formatting IP addresses in PHP can lead to security vulnerabilities such as injection attacks or unexpected behavior in your application. To prevent this, you should always validate and format IP addresses before using them in any context.

// Validate and format an IP address in PHP
$ip = "192.168.1.1";

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)){
    $formatted_ip = inet_pton($ip); // Convert IP address to binary format
    echo "Formatted IP: " . $formatted_ip;
} else {
    echo "Invalid IP address";
}