3v4l.org

run code in 300+ PHP versions simultaneously
<?php function ip_in_range($ip, $range) { $array_range = array(); if(strpos($range, ",") !== false) { // Varias faixas de ip $return = true; $array_range = explode(",", $range); foreach ($array_range as $rangeIP) { echo $rangeIP."\n"; if($rangeIP){ echo "1"."\n"; if($ip == $rangeIP){ echo "2"."\n"; $return = true; break; } else if (strpos($rangeIP, '/') !== false) { echo "3"."\n"; // $rangeIP is in IP/NETMASK format list($rangeIP, $netmask) = explode('/', $rangeIP, 2); if (strpos($netmask, '.') !== false) { echo "4"."\n"; // $netmask is a 255.255.0.0 format $netmask = str_replace('*', '0', $netmask); $netmask_dec = ip2long($netmask); $return = ( (ip2long($ip) & $netmask_dec) == (ip2long($rangeIP) & $netmask_dec) ); if($return) { echo "5"."\n"; break; } } else { echo "6"."\n"; // $netmask is a CIDR size block // fix the rangeIP argument $x = explode('.', $rangeIP); while(count($x)<4) $x[] = '0'; list($a,$b,$c,$d) = $x; $rangeIP = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d); $range_dec = ip2long($rangeIP); $ip_dec = ip2long($ip); # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0')); # Strategy 2 - Use math to create it $wildcard_dec = pow(2, (32-$netmask)) - 1; $netmask_dec = ~ $wildcard_dec; $return = (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec)); if($return) { echo "7"."\n"; break; } } } else { echo "8"."\n"; // rangeIP might be 255.255.*.* or 1.2.3.0-1.2.3.255 if (strpos($rangeIP, '*') !== false) { // a.b.*.* format echo "9"."\n"; // Just convert to A-B format by setting * to 0 for A and 255 for B $lower = str_replace('*', '0', $rangeIP); $upper = str_replace('*', '255', $rangeIP); $rangeIP = "$lower-$upper"; } if (strpos($rangeIP, '-') !== false) { // A-B format echo "10"."\n"; list($lower, $upper) = explode('-', $rangeIP, 2); $lower_dec = (float)sprintf("%u",ip2long($lower)); $upper_dec = (float)sprintf("%u",ip2long($upper)); $ip_dec = (float)sprintf("%u",ip2long($ip)); $return = ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); if($return) { echo "11"."\n"; break; } } echo "12"."\n"; $return = false; } } } echo "13"."\n"; return $return; } else { // Uma faixa de ip if(!$range){ return true; } else if($ip == $range){ return true; } else if (strpos($range, '/') !== false) { // $range is in IP/NETMASK format list($range, $netmask) = explode('/', $range, 2); if (strpos($netmask, '.') !== false) { // $netmask is a 255.255.0.0 format $netmask = str_replace('*', '0', $netmask); $netmask_dec = ip2long($netmask); return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) ); } else { // $netmask is a CIDR size block // fix the range argument $x = explode('.', $range); while(count($x)<4) $x[] = '0'; list($a,$b,$c,$d) = $x; $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d); $range_dec = ip2long($range); $ip_dec = ip2long($ip); # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0')); # Strategy 2 - Use math to create it $wildcard_dec = pow(2, (32-$netmask)) - 1; $netmask_dec = ~ $wildcard_dec; return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec)); } } else { // range might be 255.255.*.* or 1.2.3.0-1.2.3.255 if (strpos($range, '*') !==false) { // a.b.*.* format // Just convert to A-B format by setting * to 0 for A and 255 for B $lower = str_replace('*', '0', $range); $upper = str_replace('*', '255', $range); $range = "$lower-$upper"; } if (strpos($range, '-')!==false) { // A-B format list($lower, $upper) = explode('-', $range, 2); $lower_dec = (float)sprintf("%u",ip2long($lower)); $upper_dec = (float)sprintf("%u",ip2long($upper)); $ip_dec = (float)sprintf("%u",ip2long($ip)); return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); } return false; } } } echo "\n". (int) ip_in_range("127.0.0.1/25", "127.0.0.0,127.0.0.9,127.0.0.2-127.0.0.7");

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0040.01116.75
8.3.50.0090.01222.09
8.3.40.0070.01418.88
8.3.30.0040.01118.96
8.3.20.0080.00020.38
8.3.10.0080.00023.79
8.3.00.0080.00019.28
8.2.180.0120.00618.50
8.2.170.0070.01122.96
8.2.160.0150.00422.30
8.2.150.0000.01024.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0000.00817.63
8.2.110.0060.00319.42
8.2.100.0130.00018.05
8.2.90.0040.00419.46
8.2.80.0030.00617.97
8.2.70.0080.00017.75
8.2.60.0000.01018.18
8.2.50.0000.00818.07
8.2.40.0030.00519.71
8.2.30.0040.00420.68
8.2.20.0040.00417.99
8.2.10.0020.00518.25
8.2.00.0060.00318.36
8.1.280.0070.01125.92
8.1.270.0060.00324.04
8.1.260.0070.00026.35
8.1.250.0070.00028.09
8.1.240.0090.00019.56
8.1.230.0040.00817.91
8.1.220.0040.00418.04
8.1.210.0030.00618.77
8.1.200.0060.00317.48
8.1.190.0060.00317.48
8.1.180.0040.00418.10
8.1.170.0030.00518.83
8.1.160.0000.00722.23
8.1.150.0000.00718.98
8.1.140.0040.00419.81
8.1.130.0000.00717.98
8.1.120.0000.00717.66
8.1.110.0040.00417.52
8.1.100.0050.00317.63
8.1.90.0040.00417.69
8.1.80.0040.00417.54
8.1.70.0030.00317.52
8.1.60.0040.00417.77
8.1.50.0090.00017.72
8.1.40.0040.00417.57
8.1.30.0080.00017.82
8.1.20.0000.00717.66
8.1.10.0000.00817.67
8.1.00.0080.00017.70
8.0.300.0080.00018.77
8.0.290.0070.00016.88
8.0.280.0040.00418.75
8.0.270.0030.00317.34
8.0.260.0000.00717.35
8.0.250.0040.00417.16
8.0.240.0030.00517.18
8.0.230.0040.00417.17
8.0.220.0050.00216.91
8.0.210.0040.00416.98
8.0.200.0000.00717.10
8.0.190.0040.00417.14
8.0.180.0040.00417.16
8.0.170.0080.00017.11
8.0.160.0040.00417.02
8.0.150.0070.00016.91
8.0.140.0030.00516.90
8.0.130.0000.00613.45
8.0.120.0040.00417.07
8.0.110.0040.00417.10
8.0.100.0000.00817.09
8.0.90.0050.00216.95
8.0.80.0080.00817.04
8.0.70.0050.00317.04
8.0.60.0070.00016.95
8.0.50.0000.00717.05
8.0.30.0120.00717.05
8.0.20.0120.01017.42
8.0.10.0000.00717.26
8.0.00.0090.01017.04
7.4.330.0020.00215.18
7.4.320.0040.00416.78
7.4.300.0030.00316.73
7.4.290.0000.00716.67
7.4.280.0060.00316.79
7.4.270.0000.00716.74
7.4.260.0040.00416.79
7.4.250.0000.00716.57
7.4.240.0040.00416.78
7.4.230.0000.00716.43
7.4.220.0120.01216.73
7.4.210.0060.00816.76
7.4.200.0040.00416.80
7.4.160.0040.01216.46
7.4.150.0130.00917.40
7.4.140.0080.01017.86
7.4.130.0170.01516.82
7.4.120.0090.00916.80
7.4.110.0110.00716.82
7.4.100.0070.01116.65
7.4.90.0150.00316.78
7.4.80.0140.00919.39
7.4.70.0120.00916.74
7.4.60.0070.01016.59
7.4.50.0000.00916.34
7.4.40.0120.01116.66
7.4.30.0150.00616.65
7.4.00.0090.00915.16
7.3.330.0000.00613.38
7.3.320.0030.00313.55
7.3.310.0030.00316.40
7.3.300.0080.00016.37
7.3.290.0050.00316.44
7.3.280.0060.01316.56
7.3.270.0090.00917.40
7.3.260.0150.00616.55
7.3.240.0070.01116.67
7.3.230.0070.01316.42
7.3.210.0120.00616.63
7.3.200.0180.00016.81
7.3.190.0090.00816.82
7.3.180.0100.00616.82
7.3.170.0110.01116.68
7.3.160.0120.00416.48
7.2.330.0120.01216.88
7.2.320.0100.01016.94
7.2.310.0070.01016.64
7.2.300.0060.01016.68
7.2.290.0200.00716.89
7.2.60.0060.01216.89
7.2.00.0030.00919.30
7.1.200.0100.00315.91
7.1.100.0000.01418.15
7.1.70.0030.00316.99
7.1.60.0090.00919.43
7.1.50.0060.00917.13
7.1.00.0070.07322.57
7.0.200.0850.00914.96
7.0.100.0030.04320.14
7.0.90.0070.04720.00
7.0.80.0070.04720.05
7.0.70.0070.04020.07
7.0.60.0030.04020.21
7.0.50.0000.04320.52
7.0.40.0000.07020.07
7.0.30.0030.04320.13
7.0.20.0030.06720.18
7.0.10.0100.03720.20
7.0.00.0100.06320.11
5.6.280.0030.07021.21
5.6.250.0100.03320.88
5.6.240.0130.03020.79
5.6.230.0100.03720.78
5.6.220.0030.04020.59
5.6.210.0070.04020.77
5.6.200.0100.03721.14
5.6.190.0000.04721.27
5.6.180.0100.03721.15
5.6.170.0030.07021.03
5.6.160.0100.06721.23
5.6.150.0100.08021.15
5.6.140.0130.07721.09
5.6.130.0030.08721.20
5.6.120.0100.04721.14
5.6.110.0100.06021.26
5.6.100.0100.07721.15
5.6.90.0100.07321.14
5.6.80.0030.08020.41
5.6.70.0070.04320.54
5.6.60.0200.06720.52
5.6.50.0100.06320.52
5.6.40.0170.06020.59
5.6.30.0100.05320.39
5.6.20.0130.06020.55
5.6.10.0070.08020.43
5.6.00.0130.07020.39
5.5.380.0030.06020.49
5.5.370.0030.04020.54
5.5.360.0000.04320.43
5.5.350.0000.04320.52
5.5.340.0070.04020.96
5.5.330.0000.05721.02
5.5.320.0070.04020.87
5.5.310.0100.04020.85
5.5.300.0000.08720.92
5.5.290.0030.08320.90
5.5.280.0130.07720.98
5.5.270.0070.04720.97
5.5.260.0100.07720.87
5.5.250.0070.07020.79
5.5.240.0070.07720.39
5.5.230.0100.08020.27
5.5.220.0070.06720.30
5.5.210.0130.07020.36
5.5.200.0100.05720.34
5.5.190.0000.07020.28
5.5.180.0100.07720.25
5.5.160.0100.07320.27
5.5.150.0000.04720.37
5.5.140.0070.08020.17
5.5.130.0130.04020.22
5.5.120.0100.07020.17
5.5.110.0230.02720.34
5.5.100.0070.06320.25
5.5.90.0070.07020.17
5.5.80.0000.05020.11
5.5.70.0130.03720.09
5.5.60.0030.07720.24
5.5.50.0030.07320.19
5.5.40.0100.04320.14
5.5.30.0070.07020.15
5.5.20.0070.06320.02
5.5.10.0100.04320.09
5.5.00.0170.06020.13
5.4.450.0030.07719.58
5.4.440.0100.07319.42
5.4.430.0070.05319.43
5.4.420.0170.05719.55
5.4.410.0030.08019.48
5.4.400.0070.07719.21
5.4.390.0070.08018.91
5.4.380.0000.07319.18
5.4.370.0070.05719.25
5.4.360.0030.07319.23
5.4.350.0130.07019.19
5.4.340.0100.05319.11
5.4.320.0100.05318.97
5.4.310.0030.05019.27
5.4.300.0130.06719.24
5.4.290.0130.07319.27
5.4.280.0070.07319.09
5.4.270.0130.05719.11
5.4.260.0200.06019.15
5.4.250.0070.04019.14
5.4.240.0130.06019.20
5.4.230.0070.07719.09
5.4.220.0130.06719.19
5.4.210.0130.07319.01
5.4.200.0070.04718.94
5.4.190.0070.06719.01
5.4.180.0100.07319.28
5.4.170.0000.06719.10
5.4.160.0100.03019.20
5.4.150.0130.06719.12
5.4.140.0030.06716.52
5.4.130.0030.05716.54
5.4.120.0070.07716.57
5.4.110.0100.06016.46
5.4.100.0100.07016.48
5.4.90.0030.07716.59
5.4.80.0070.07316.62
5.4.70.0070.06016.45
5.4.60.0070.04716.57
5.4.50.0100.06316.58
5.4.40.0070.07016.48
5.4.30.0030.07716.56
5.4.20.0000.04716.61
5.4.10.0030.07716.56
5.4.00.0130.05015.90

preferences:
58.02 ms | 401 KiB | 5 Q