3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getKey($current, $step): int { $keys = [1,2,3,4,5,6,7]; $currentKey = array_search($current, $keys); $size = count($keys); // normalize step offset $step = ($step % $size) + $size; // ^-----------^ ^-----^ // │ └ move possible negative to positive // └ get a value from -7 to +7 // add offset for current key $newKey = ($currentKey + $step) % $size; // ^-----------------^ ^-----^ // │ └ wrap around in case we exceed $size // └ add normalized step offset to new element to current return $keys[$newKey]; } // Tests echo getKey(1,-1) . PHP_EOL; echo getKey(3,1) . PHP_EOL; echo getKey(7,1) . PHP_EOL; echo getKey(7,15) . PHP_EOL; // same as +1 echo getKey(1,-8) . PHP_EOL; // same as -1 echo getKey(1,-15) . PHP_EOL; // same as -1

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.4.20.0160.00317.95
8.4.10.0060.00322.35
8.3.150.0150.00317.54
8.3.140.0150.00416.92
8.3.130.0070.00416.69
8.3.120.0060.00318.86
8.3.110.0050.00320.94
8.3.100.0090.00024.06
8.3.90.0050.00326.77
8.3.80.0090.00016.50
8.3.70.0130.00916.58
8.3.60.0140.00616.75
8.3.50.0150.00323.64
8.3.40.0140.00018.71
8.3.30.0150.00018.73
8.3.20.0030.00524.18
8.3.10.0060.00324.66
8.3.00.0040.00726.16
8.2.270.0100.01016.91
8.2.260.0100.00718.60
8.2.250.0040.00418.43
8.2.240.0060.00318.91
8.2.230.0120.00322.58
8.2.220.0060.00337.54
8.2.210.0060.00326.77
8.2.200.0060.00316.63
8.2.190.0110.00716.63
8.2.180.0180.00025.92
8.2.170.0110.00419.04
8.2.160.0040.01122.96
8.2.150.0040.00425.66
8.2.140.0060.00324.66
8.2.130.0070.00026.16
8.2.120.0000.00721.25
8.2.110.0030.00719.27
8.2.100.0080.00417.97
8.2.90.0030.00517.88
8.2.80.0090.00017.97
8.2.70.0000.00818.05
8.2.60.0060.00317.93
8.2.50.0080.00018.07
8.2.40.0040.00418.16
8.2.30.0050.00218.07
8.2.20.0040.00418.02
8.2.10.0000.00719.33
8.2.00.0030.00519.25
8.1.310.0070.01118.48
8.1.300.0000.00918.51
8.1.290.0080.00018.88
8.1.280.0090.00625.92
8.1.270.0080.00023.99
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0030.00622.68
8.1.230.0110.00020.88
8.1.220.0000.00817.79
8.1.210.0040.00418.77
8.1.200.0050.00317.22
8.1.190.0000.00817.22
8.1.180.0050.00318.10
8.1.170.0040.00418.71
8.1.160.0030.00518.97
8.1.150.0000.00718.84
8.1.140.0030.00520.25
8.1.130.0040.00419.23
8.1.120.0060.00317.51
8.1.110.0040.00417.48
8.1.100.0000.00817.47
8.1.90.0050.00217.51
8.1.80.0000.00717.39
8.1.70.0000.00717.38
8.1.60.0000.00817.60
8.1.50.0060.00317.58
8.1.40.0000.00717.54
8.1.30.0030.00517.65
8.1.20.0080.00017.63
8.1.10.0040.00417.65
8.1.00.0040.00417.48
8.0.300.0000.00718.77
8.0.290.0040.00416.88
8.0.280.0000.00718.42
8.0.270.0000.00718.05
8.0.260.0000.00716.84
8.0.250.0030.00316.92
8.0.240.0070.00017.04
8.0.230.0000.00717.03
8.0.220.0030.00616.94
8.0.210.0040.00416.86
8.0.200.0030.00516.98
8.0.190.0000.01017.05
8.0.180.0070.00016.98
8.0.170.0040.00416.84
8.0.160.0070.00016.83
8.0.150.0000.00716.91
8.0.140.0040.00416.88
8.0.130.0000.00513.29
8.0.120.0000.00716.79
8.0.110.0050.00316.92
8.0.100.0040.00416.99
8.0.90.0040.00416.87
8.0.80.0090.00616.91
8.0.70.0040.00416.96
8.0.60.0070.00016.93
8.0.50.0080.00016.96
8.0.30.0070.01017.05
8.0.20.0090.01317.05
8.0.10.0000.00716.84
8.0.00.0070.01116.75
7.4.330.0050.00015.55
7.4.320.0030.00316.55
7.4.300.0060.00016.59
7.4.290.0090.00016.55
7.4.280.0000.00816.48
7.4.270.0050.00216.60
7.4.260.0030.00313.30
7.4.250.0050.00216.56
7.4.240.0040.00416.60
7.4.230.0070.00016.54
7.4.220.0030.00416.65
7.4.210.0070.01116.64
7.4.200.0050.00216.66
7.4.130.0100.00916.47
7.4.120.0070.01316.55
7.4.110.0060.01316.73
7.4.100.0030.01516.74
7.4.90.0120.00616.34
7.4.80.0090.01616.48
7.4.70.0040.02116.59
7.4.60.0140.00916.44
7.4.50.0130.01016.29
7.4.40.0060.01616.32
7.4.30.0140.01116.64
7.4.20.0040.01416.41
7.4.10.0070.01116.73
7.4.00.0100.00716.39
7.3.330.0040.00416.37
7.3.320.0000.00613.29
7.3.310.0000.00716.16
7.3.300.0000.00816.19
7.3.290.0090.00716.34
7.3.280.0000.01416.28
7.3.260.0130.00816.59
7.3.230.0120.00616.41
7.3.210.0060.01116.36
7.3.200.0060.01816.37
7.3.190.0100.01416.61
7.3.180.0070.01016.37
7.3.170.0150.00316.60
7.3.160.0060.01216.29
7.3.150.0090.01216.57
7.3.140.0110.01116.54
7.3.130.0130.00916.43
7.3.120.0100.00716.47
7.3.110.0150.00316.32
7.3.100.0090.00916.56
7.3.90.0110.01116.57
7.3.80.0000.01716.27
7.3.70.0070.01016.38
7.3.60.0030.01316.35
7.3.50.0110.00616.49
7.3.40.0100.00716.26
7.3.30.0110.01516.43
7.3.20.0090.00916.48
7.3.10.0120.00616.46
7.3.00.0150.00416.44
7.2.330.0060.01216.42
7.2.320.0080.01716.66
7.2.310.0170.00416.84
7.2.300.0100.01316.74
7.2.290.0130.01316.85
7.2.280.0090.00916.71
7.2.270.0060.01216.25
7.2.260.0100.01416.41
7.2.250.0070.01716.93
7.2.240.0120.01216.59
7.2.230.0140.01116.70
7.2.220.0070.01716.38
7.2.210.0140.00416.43
7.2.200.0000.01716.65
7.2.190.0030.01416.46
7.2.180.0170.00016.77
7.2.170.0160.01016.51
7.2.160.0090.00916.63
7.2.150.0150.00416.59
7.2.140.0130.00716.71
7.2.130.0040.01616.54
7.2.120.0100.01016.69
7.2.110.0110.00816.75
7.2.100.0130.00616.88
7.2.90.0100.00916.47
7.2.80.0220.00916.61
7.2.70.0120.00616.59
7.2.60.0130.00616.66
7.2.50.0130.00616.58
7.2.40.0060.01316.63
7.2.30.0100.01016.58
7.2.20.0130.00616.41
7.2.10.0100.01016.56
7.2.00.0060.01416.54

preferences:
34.98 ms | 403 KiB | 5 Q