How can PHP scripts be optimized to efficiently scan and log IP addresses without impacting performance?

To efficiently scan and log IP addresses in PHP without impacting performance, it is important to minimize unnecessary processing and database interactions. One way to optimize this is by using a data structure like a HashSet to store unique IP addresses and only log new ones. Additionally, consider using efficient algorithms and data structures for IP address validation and comparison.

// Initialize a HashSet to store unique IP addresses
$ipSet = [];

// Function to log IP addresses efficiently
function logIPAddress($ip) {
    global $ipSet;

    // Check if IP address is not already logged
    if (!isset($ipSet[$ip])) {
        // Log the IP address
        $ipSet[$ip] = true;
        // Perform any additional logging operations
    }
}

// Example usage
$logIP = '192.168.1.1';
logIPAddress($logIP);