Are there any best practices for handling IP address validation in PHP?

IP address validation in PHP can be done using the filter_var function with the FILTER_VALIDATE_IP flag. This function will validate if the provided IP address is in a valid format. It is a best practice to use this function to ensure that the input is a valid IP address before further processing.

$ip = "192.168.1.1";

if(filter_var($ip, FILTER_VALIDATE_IP)) {
    echo "Valid IP address";
} else {
    echo "Invalid IP address";
}