run code in 300+ PHP versions simultaneously
<?php /** * Generate a random string, using a cryptographically secure * pseudorandom number generator (random_int) * * For PHP 7, random_int is a PHP core function * For PHP 5.x, depends on https://github.com/paragonie/random_compat * * @param int $length How many characters do we want? * @param string $keyspace A string of all possible characters * to select from * @return string */ function random_str( int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ): string { if ($length < 1) { throw new \RangeException("Length must be a positive integer"); } $pieces = []; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } return implode('', $pieces); } $a = random_str(4, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); $b = random_str(10, '123456789'); $c = random_str(); var_dump($a, $b, $c);
based on f7tVG

Shows result from various feature-branches currently under review from the php developers. Contact me to have additional branches featured.

Output for branch master
string(4) "NVMA" string(10) "6281419893" string(64) "gCAVKo8LS53eIWgdhODlUWlwrIh1ls6h8S7wCqlswzEYz8WLfazkTkydFdSx3mvR"
Output for branch master_jit
string(4) "MGEE" string(10) "6632753958" string(64) "erKvEqYr1qVPZSTbU3I66jVETKVpq5Mnr69JfzU1hvDGtVoIcxyZCwDLiiudpsc3"

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page