How does a router handle IP addresses for multiple devices accessing the internet?

A router uses Network Address Translation (NAT) to assign a unique private IP address to each device on the local network. When these devices access the internet, the router translates their private IP addresses to a single public IP address. This allows multiple devices to share the same public IP address while maintaining separate private IP addresses for communication within the local network.

// Sample PHP code for handling IP addresses with NAT on a router
$localIP = "192.168.1.100"; // Private IP address of the device
$publicIP = "203.0.113.1"; // Public IP address assigned by the router

// Perform NAT translation for outgoing traffic
function performNATTranslation($localIP, $publicIP) {
    // Translate the local IP address to the public IP address
    echo "Translated IP address: " . $publicIP;
}

// Example usage
performNATTranslation($localIP, $publicIP);