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') { $pieces = []; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } return implode('', $pieces); } 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 Typed properties v2
string(32) "iaiiBMZB4aBcVKS9rR1t9I6MOQTdRsqC" string(8) "zoqeahbh"
Output for Arrow functions
string(32) "F0MKKjE9IifXyTYAkWOQ2jIyiH6oW2td" string(8) "gbgeuasd"
Output for Object typehint
string(32) "KYbaGHUoRvYEiZ647H5FkN5jxEkffCSQ" string(8) "cnvdgmqc"
Output for Array change keys
string(32) "zJt9qLSr0PPDQNWymRH4v5nV5QuvW8w8" string(8) "tltcqunm"
Output for Lexical anon
Fatal error: Call to undefined function random_int() in /in/b4PST on line 20

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