3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $str = ''; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $str .= $keyspace[random_int(0, $max)]; } return $str; } var_dump(random_str(32)); var_dump(random_str(8, 'abcdefghijklmnopqrstuvwxyz'));

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

Output for Arrow functions
string(32) "CtMrZ0r9liho8yV1mp4qEMeQjXYSdqgy" string(8) "iwjwiecp"
Output for Object typehint
string(32) "cgRCFxHAt1XpjnXHuDYQWyMlOPJLpPWK" string(8) "mpwmbsqh"
Output for Array change keys
string(32) "GJKRVrdmF4MGhSBsuB1h14nU5vt48Clf" string(8) "bheyiefn"
Output for Lexical anon
Fatal error: Call to undefined function random_int() in /in/KKlc3 on line 20
Output for Typed properties
string(32) "HBl9GHd5ifZXtbOmbfvZPPILROTjj61Q" string(8) "oxoruvjy"

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