3v4l.org

run code in 300+ PHP versions simultaneously
<?php function ipv6_addr_in_subnet($addr, $subnet, $mask = null) { $addrBin = @inet_pton($addr); if (!$addrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $addr); } $cidr = 0; if (isset($mask)) { if (is_int($mask)) { $cidr = (int)$mask; if ($cidr < 0 || $cidr > 128) { throw new \InvalidArgumentException('Invalid IPv6 CIDR mask: ' . $mask); } } else { $maskBin = @inet_pton($mask); if (!$maskBin) { throw new \InvalidArgumentException('Invalid IPv6 mask: ' . $mask); } } $subnetAddr = $subnet; } else { $parts = explode('/', $subnet); $numParts = count($parts); if ($numParts === 1) { $maskBin = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; } else if ($numParts === 2) { $cidr = (int)$parts[1]; } else { throw new \InvalidArgumentException('Invalid IPv6 subnet'); } $subnetAddr = $parts[0]; } if (!isset($maskBin)) { $blocks = (int)($cidr / 16); $maskBin = pack( 'n*', ...array_fill(0, $blocks, 0xffff), ...array_pad([((2 ** (16 - ($cidr % 16))) - 1) ^ 0xffff], 8 - $blocks, 0) ); } $subnetAddrBin = @inet_pton($subnetAddr); if (!$subnetAddrBin) { throw new \InvalidArgumentException('Invalid IPv6 address: ' . $subnetAddr); } return ($addrBin & $maskBin) === ($subnetAddrBin & $maskBin); } var_dump(ipv6_addr_in_subnet('2001::1', '2001::/16')); var_dump(ipv6_addr_in_subnet('2001::1', '2000::/17')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/15')); var_dump(ipv6_addr_in_subnet('2001::1', '2001::/120'));
Output for 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
bool(true) bool(false) bool(true) bool(true)
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 35 Parse error: syntax error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/T2jeX on line 35 Parse error: parse error, unexpected '.' in /in/T2jeX on line 45
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/T2jeX on line 7
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_NEW in /in/T2jeX on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/T2jeX on line 7
Process exited with code 255.

preferences:
213.84 ms | 401 KiB | 343 Q