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('::1', '::/0')); var_dump(ipv6_addr_in_subnet('::1', '::/128'));
Output for 5.6.0 - 5.6.4, 5.6.7 - 5.6.12, 5.6.14 - 5.6.24, 5.6.26, 5.6.28, 5.6.35 - 5.6.40, 7.0.2 - 7.0.3, 7.0.6 - 7.0.9, 7.0.11, 7.0.14, 7.0.17, 7.0.20 - 7.0.30, 7.0.32 - 7.0.33, 7.1.2, 7.1.4, 7.1.6 - 7.1.12, 7.1.15 - 7.1.16, 7.1.21 - 7.1.33, 7.2.0 - 7.2.3, 7.2.10 - 7.2.26, 7.3.0 - 7.3.13, 7.4.0 - 7.4.1, 8.1.27 - 8.1.28, 8.2.14 - 8.2.18, 8.3.2 - 8.3.4, 8.3.6
bool(true) bool(false)
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 bool(true) bool(false)
Output for 5.5.36, 5.6.5 - 5.6.6, 5.6.13, 5.6.25, 5.6.27, 5.6.29 - 5.6.34, 7.0.0 - 7.0.1, 7.0.4 - 7.0.5, 7.0.10, 7.0.12 - 7.0.13, 7.0.15 - 7.0.16, 7.0.18 - 7.0.19, 7.0.31, 7.1.0 - 7.1.1, 7.1.3, 7.1.5, 7.1.13 - 7.1.14, 7.1.17 - 7.1.20, 7.2.4 - 7.2.9

Process exited with code 137.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.35, 5.5.37 - 5.5.38
Parse error: syntax error, unexpected '.' in /in/CaoJO 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/CaoJO on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 35 Parse error: syntax error, unexpected '.' in /in/CaoJO 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/CaoJO on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 21 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/CaoJO on line 35 Parse error: parse error, unexpected '.' in /in/CaoJO 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/CaoJO 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/CaoJO on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/CaoJO on line 7
Process exited with code 255.

preferences:
229.92 ms | 401 KiB | 337 Q