3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Generate a random string * * @link https://paragonie.com/b/JvICXzh_jhLyt4y3 * * @param int $length - How long should our random string be? * @param string $charset - A string of all possible characters to choose from * @return string */ function random_str($length = 32, $charset = 'abcdefghijklmnopqrstuvwxyz') { // Type checks: if (!is_numeric($length)) { throw new InvalidArgumentException( 'random_str - Argument 1 - expected an integer' ); } if (!is_string($charset)) { throw new InvalidArgumentException( 'random_str - Argument 2 - expected a string' ); } if ($length < 1) { // Just return an empty string. Any value < 1 is meaningless. return ''; } // Remove duplicate characters from $charset $split = str_split($charset); $charset = implode('', array_unique($split)); // This is the maximum index for all of the characters in the string $charset $charset_max = strlen($charset) - 1; if ($charset_max < 1) { // Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...' throw new LogicException( 'random_str - Argument 2 - expected a string that contains at least 2 distinct characters' ); } // Now that we have good data, this is the meat of our function: $random_str = ''; for ($i = 0; $i < $length; ++$i) { $r = random_int(0, $charset_max); $random_str .= $charset[$r]; } return $random_str; } /* ------------------------------------------------------------------------ */ var_dump(random_str(10)); var_dump(random_str(16, '0123456789')); var_dump(random_str(8, '0123456789abcdef')); var_dump(random_str(24, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.'));

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)
7.4.10.0070.01315.13
7.4.00.0130.00714.89
7.3.130.0090.00914.78
7.3.120.0040.00814.91
7.3.110.0060.01314.96
7.3.100.0040.01115.13
7.3.90.0030.01014.68
7.3.80.0040.01115.03
7.3.70.0080.00814.55
7.3.60.0040.01114.61
7.3.50.0110.00414.63
7.3.40.0000.01414.63
7.3.30.0070.00614.64
7.3.20.0000.01616.38
7.3.10.0050.00816.71
7.3.00.0050.00616.75
7.2.260.0070.01314.97
7.2.250.0080.01014.83
7.2.240.0060.00915.34
7.2.230.0030.01215.26
7.2.220.0060.00615.07
7.2.210.0040.01115.27
7.2.200.0000.01015.10
7.2.190.0110.00415.01
7.2.180.0070.00314.93
7.2.170.0070.00715.22
7.2.160.0030.00914.83
7.2.150.0030.01417.00
7.2.140.0060.00917.04
7.2.130.0060.00716.98
7.2.120.0070.00717.02
7.2.110.0050.01016.80
7.2.100.0020.01216.94
7.2.90.0050.00816.98
7.2.80.0080.00816.79
7.2.70.0070.00716.88
7.2.60.0080.00516.88
7.2.50.0050.00816.75
7.2.40.0050.00616.86
7.2.30.0050.00916.90
7.2.20.0040.00917.02
7.2.10.0090.00516.92
7.2.00.0030.00917.55
7.1.330.0030.01015.90
7.1.320.0090.00915.57
7.1.310.0000.01215.94
7.1.300.0070.00715.71
7.1.290.0000.01515.66
7.1.280.0000.01215.77
7.1.270.0030.01115.65
7.1.260.0000.01616.00
7.1.250.0060.00715.84
7.1.240.0030.01015.82
7.1.230.0040.00615.81
7.1.220.0030.00915.67
7.1.210.0120.00415.73
7.1.200.0100.00515.96
7.1.190.0030.01015.69
7.1.180.0080.00315.87
7.1.170.0100.00615.70
7.1.160.0030.00915.74
7.1.150.0090.00415.74
7.1.140.0040.00715.81
7.1.130.0040.00815.79
7.1.120.0030.00915.94
7.1.110.0040.00715.85
7.1.100.0070.00415.89
7.1.90.0060.00915.81
7.1.80.0020.00915.80
7.1.70.0040.01016.21
7.1.60.0070.00616.26
7.1.50.0060.00816.01
7.1.40.0060.00916.08
7.1.30.0030.00915.88
7.1.20.0020.00916.09
7.1.10.0060.00615.94
7.1.00.0020.01116.05
7.0.330.0090.00515.44
7.0.320.0050.00715.35
7.0.310.0040.01215.23
7.0.300.0060.00415.28
7.0.290.0060.00715.48
7.0.280.0040.01015.49
7.0.270.0070.00515.52
7.0.260.0100.00315.30
7.0.250.0030.01015.44
7.0.240.0020.00815.23
7.0.230.0040.00615.41
7.0.220.0050.00515.46
7.0.210.0060.00615.51
7.0.200.0070.00415.90
7.0.190.0070.00415.70
7.0.180.0080.00615.77
7.0.170.0040.00715.79
7.0.160.0070.00315.60
7.0.150.0070.00315.82
7.0.140.0050.00615.73
7.0.130.0100.00415.81
7.0.120.0030.00815.72
7.0.110.0050.00715.80
7.0.100.0070.00715.78
7.0.90.0040.00815.77
7.0.80.0060.00715.82
7.0.70.0030.00915.62
7.0.60.0070.00515.66
7.0.50.0020.00915.71
7.0.40.0060.00413.99
7.0.30.0080.00514.06
7.0.20.0120.00014.09
7.0.10.0050.00714.00
7.0.00.0060.00513.94
5.6.400.0070.00714.45
5.6.390.0030.01014.60
5.6.380.0050.00414.19
5.6.370.0030.00714.34
5.6.360.0070.00614.52
5.6.350.0030.00914.49
5.6.340.0070.00514.41
5.6.330.0050.01214.46
5.6.320.0030.01114.53
5.6.310.0050.01014.19
5.6.300.0040.01916.45
5.6.290.0040.01916.47
5.6.280.0060.01716.41
5.6.270.0020.02016.36
5.6.260.0010.02216.61
5.6.250.0080.02616.43
5.6.240.0070.02216.50
5.6.230.0050.02016.53
5.6.220.0060.03416.46
5.6.210.0090.02216.40
5.6.200.0050.02516.48
5.6.190.0050.02016.45
5.6.180.0040.01916.38
5.6.170.0020.02216.30
5.6.160.0040.01916.38
5.6.150.0050.03216.55
5.6.140.0050.02016.49
5.6.130.0050.01716.52
5.6.120.0060.02016.54
5.6.110.0060.01916.44
5.6.100.0030.02016.29
5.6.90.0060.02116.43
5.6.80.0020.02116.29
5.6.70.0050.01716.11
5.6.60.0060.02016.24
5.6.50.0060.01716.09
5.6.40.0110.02616.18
5.6.30.0040.01916.29
5.6.20.0050.02916.20
5.6.10.0020.02116.15
5.6.00.0060.01916.22
5.5.380.0030.02314.35
5.5.370.0080.02314.40
5.5.360.0030.04614.35
5.5.350.0100.02014.32
5.5.340.0050.02214.62
5.5.330.0020.02714.64
5.5.320.0020.02514.45
5.5.310.0030.02614.64
5.5.300.0020.03114.63
5.5.290.0020.02814.59
5.5.280.0020.02414.62
5.5.270.0050.02314.67
5.5.260.0050.02614.67
5.5.250.0030.02314.54
5.5.240.0030.02714.36
5.5.230.0030.02414.29
5.5.220.0050.02414.28
5.5.210.0060.02314.21
5.5.200.0050.02714.21
5.5.190.0050.02114.13
5.5.180.0070.02314.13
5.5.170.0110.00011.18
5.5.160.0030.02114.12
5.5.150.0140.01814.30
5.5.140.0000.04014.26
5.5.130.0030.03014.33
5.5.120.0020.02414.33
5.5.110.0040.02614.34
5.5.100.0020.02814.22
5.5.90.0040.02114.24
5.5.80.0030.02514.13
5.5.70.0020.02514.26
5.5.60.0020.02314.27
5.5.50.0050.01814.25
5.5.40.0010.02214.23
5.5.30.0050.02014.25
5.5.20.0030.02314.26
5.5.10.0020.02514.25
5.5.00.0060.02114.07
5.4.450.0050.02615.24
5.4.440.0090.02115.34
5.4.430.0020.02515.33
5.4.420.0010.03115.29
5.4.410.0020.02415.14
5.4.400.0030.02515.09
5.4.390.0030.02715.07
5.4.380.0050.01915.02
5.4.370.0020.02215.02
5.4.360.0020.02415.12
5.4.350.0030.02415.02
5.4.340.0020.02415.02
5.4.330.0060.00611.18
5.4.320.0020.02615.03
5.4.310.0060.02015.10
5.4.300.0000.02215.02
5.4.290.0050.02215.09
5.4.280.0020.04515.01
5.4.270.0050.02615.08
5.4.260.0030.02115.10
5.4.250.0080.02115.14
5.4.240.0040.02315.02
5.4.230.0030.02415.03
5.4.220.0040.02115.02
5.4.210.0050.02115.09
5.4.200.0030.02415.09
5.4.190.0050.02115.13
5.4.180.0070.01915.09
5.4.170.0030.03815.13
5.4.160.0060.01915.03
5.4.150.0030.02315.01
5.4.140.0010.02913.85
5.4.130.0060.01813.79
5.4.120.0030.02013.87
5.4.110.0050.04013.79
5.4.100.0050.02413.91
5.4.90.0050.02013.93
5.4.80.0000.02313.92
5.4.70.0040.02013.93
5.4.60.0030.02413.79
5.4.50.0150.01513.79
5.4.40.0040.02113.91
5.4.30.0050.02713.83
5.4.20.0030.02313.77
5.4.10.0000.02313.89
5.4.00.0020.02013.66
5.3.290.0030.02413.24
5.3.280.0060.01813.24
5.3.270.0030.03413.24
5.3.260.0040.03313.24
5.3.250.0050.03513.24
5.3.240.0030.02313.24
5.3.230.0020.02113.24
5.3.220.0050.01713.24
5.3.210.0030.02313.24
5.3.200.0040.02113.24
5.3.190.0020.02213.24
5.3.180.0050.02113.24
5.3.170.0020.02413.24
5.3.160.0020.02513.24
5.3.150.0030.02713.24
5.3.140.0000.02713.24
5.3.130.0020.02413.24
5.3.120.0050.02113.24
5.3.110.0010.02513.24
5.3.100.0000.02213.24
5.3.90.0050.02213.24
5.3.80.0050.02013.24
5.3.70.0060.02813.24
5.3.60.0050.02013.24
5.3.50.0030.03513.24
5.3.40.0040.01913.24
5.3.30.0010.02213.24
5.3.20.0000.02413.24
5.3.10.0020.02613.24
5.3.00.0010.02813.24
5.2.170.0020.02013.24
5.2.160.0020.02113.24
5.2.150.0020.02213.24
5.2.140.0020.01913.24
5.2.130.0020.01913.24
5.2.120.0070.01813.24
5.2.110.0020.02013.24
5.2.100.0030.01713.24
5.2.90.0020.01913.24
5.2.80.0020.01913.24
5.2.70.0020.01613.24
5.2.60.0000.02013.24
5.2.50.0030.01713.24
5.2.40.0020.01613.24
5.2.30.0030.01713.24
5.2.20.0050.01513.24
5.2.10.0020.01613.24
5.2.00.0000.02313.24
5.1.60.0040.01313.24
5.1.50.0020.01813.24
5.1.40.0010.01513.24
5.1.30.0030.01513.24
5.1.20.0030.01313.24
5.1.10.0030.02313.24
5.1.00.0020.01513.24
5.0.50.0030.01913.24
5.0.40.0060.00813.24
5.0.30.0030.03613.24
5.0.20.0030.01113.24
5.0.10.0000.01413.24
5.0.00.0020.01713.24
4.4.90.0040.00813.24
4.4.80.0000.01013.24
4.4.70.0030.00713.24
4.4.60.0000.01013.24
4.4.50.0030.00813.24
4.4.40.0020.01313.24
4.4.30.0040.00613.24
4.4.20.0030.02113.24
4.4.10.0000.01513.24
4.4.00.0050.02513.24
4.3.110.0020.01513.24
4.3.100.0010.01413.24
4.3.90.0020.00913.24
4.3.80.0000.01513.24
4.3.70.0020.00913.24
4.3.60.0010.00913.24
4.3.50.0000.01213.24
4.3.40.0000.01913.24
4.3.30.0000.01113.24
4.3.20.0020.01013.24
4.3.10.0020.00913.24
4.3.00.0020.01013.24

preferences:
38.5 ms | 401 KiB | 5 Q