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'));
Output for 7.2.2
string(32) "To41OWWAlOG2zry8BfkbHwvLka3qFWDc" string(8) "ssbbsgrv"
Output for 7.2.1
string(32) "qu6IUx7fXYUuk357nO34FppPQSOKpCt7" string(8) "sikzojad"
Output for 7.2.0
string(32) "BlBMOTe33SYQNS7ks6fxMkkzaGVk7Tcw" string(8) "tbvlbrwg"
Output for 7.1.14
string(32) "VdnbqiJcisA3xWbSiEMl8Lmq0IQuC8OV" string(8) "puqtgqzy"
Output for 7.1.13
string(32) "4hHasR0nf8U7lDlmB4PVZZ9bmFknWPXl" string(8) "kbycucge"
Output for 7.1.12
string(32) "gTqzQhL1Hej1aEu1jocM7RiTX2ZT9PMk" string(8) "dicjztrn"
Output for 7.1.11
string(32) "PHDKzF5b0L3hsCuNyuwPcwJVpEPu2FiM" string(8) "urdcblsm"
Output for 7.1.10
string(32) "E5oQ7rwQFZpTCYheW46i1z2jak4Ppk84" string(8) "bwtqnwqf"
Output for 7.1.9
string(32) "iurZCxJtdjQ4L5CIBTRENx2Nm9GYon7M" string(8) "ewaxaonu"
Output for 7.1.8
string(32) "bTUFKwaeL1cZbHoCFRTL2p1gMJ6QPOSH" string(8) "enqmtvlh"
Output for 7.1.7
string(32) "ln1qPLip4Erx86eYlFkin1NiAME1klhF" string(8) "pylrijnt"
Output for 7.1.6
string(32) "WxlWioesWpvtphyt6ppfX0XRuYOefyXR" string(8) "jdewvhxk"
Output for 7.1.5
string(32) "320EecKUZGHZiu3ZqYzslESZnm5eKXEP" string(8) "jowgonlg"
Output for 7.1.4
string(32) "9Hyg69UzfB5RMWIuhtthx6XlSFaPYzl4" string(8) "txdgjogh"
Output for 7.1.3
string(32) "oflvkvQlgLRlC8tDEw1GHhwijdkoqJmt" string(8) "ikwlzqsm"
Output for 7.1.2
string(32) "mncxwQk3dCg12DMPlSRCKmocOwQi3hfd" string(8) "apgonach"
Output for 7.1.1
string(32) "WPRN2YYNB67ZaZTuI8NHfLPqv32Jxz0z" string(8) "pzmulvkw"
Output for 7.1.0
string(32) "3VlIqt8OzxqG88R7y2UEUVSfEtNwTi7f" string(8) "gtqbhxxi"
Output for 7.0.27
string(32) "yUdpZYo8enI9k9UjD3UcW8tGUj4TsQzB" string(8) "unxywdud"
Output for 7.0.26
string(32) "FWXYjzNMPTummR1TFWaFAa5DwnTONRk1" string(8) "wxzizsan"
Output for 7.0.25
string(32) "pIPg3owIb3n5qYKiZKXGFRl8UC22BVpX" string(8) "garumoqv"
Output for 7.0.24
string(32) "nqUor9LWQKV2zqVmEB48jEIx61XzHuW2" string(8) "etcsjiqy"
Output for 7.0.23
string(32) "gACT6plTfKIHyfHw8wP1ewUrhFW7P68a" string(8) "wfwfroez"
Output for 7.0.22
string(32) "CRMIt0kJ3i00AmtzdT6lyB8ljEZP6WIq" string(8) "uxvrewxe"
Output for 7.0.21
string(32) "4gp7o3kV9MdTYbdCjHL6XG0dtUgTi3ME" string(8) "nspbltih"
Output for 7.0.20
string(32) "VpUmYSqHIt0egsle2Kq6bPhnuGeYH4ow" string(8) "geuofmyv"
Output for 7.0.19
string(32) "qnwXRmuGoCGWzGf0HUAcQWd0dObnstnY" string(8) "atpngemo"
Output for 7.0.18
string(32) "JtPksJpdw922jaSerGU6wCJkYPIUpbvb" string(8) "bzyckcds"
Output for 7.0.17
string(32) "CRENXSjSSAdhBaOUc4nGYeoG8wQf4Dbr" string(8) "rsfvsqhi"
Output for 7.0.16
string(32) "7qYFxt3REvqr6MhgT0RbnxJICDElXlsD" string(8) "dlxvzgth"
Output for 7.0.15
string(32) "rKf7HnAjJaLwbFueNCbk9P3wCdvlToxi" string(8) "kbgsywlm"
Output for 7.0.14
string(32) "ipHa7IbYJ52fDKAQ4gPXVJBqvVxagxdx" string(8) "tipbkjfk"
Output for 7.0.13
string(32) "fwu8JCJqsBt1O9QXVWmRGAUN7ZmoaVX2" string(8) "vmfzwvsb"
Output for 7.0.12
string(32) "gaBA9eK1lOLmzRYNiJrHSefqJOLnoXY4" string(8) "mccptxxx"
Output for 7.0.11
string(32) "NB9OV3Udj5wNCNl4Ed1fFkvuNssdjckf" string(8) "puipgfiw"
Output for 7.0.10
string(32) "qUKIBx7TBUA2BjroWOZzNMyEcP5B5CZK" string(8) "irqniziz"
Output for 7.0.9
string(32) "1wiNIrGwqkfHNruDHMBM6Kc1ye5Kao0D" string(8) "gtohschy"
Output for 7.0.8
string(32) "Ex01Hz0yBiUzDxdxzvh9XQL89d7x5sO7" string(8) "soiqrysf"
Output for 7.0.7
string(32) "QG4NMIzAYFtG8jibwlHCfSZCvH5vpeg4" string(8) "aoufzlbw"
Output for 7.0.6
string(32) "0pZcOGLVBOZxkUarQsbUU52NysTBaH2T" string(8) "dcfstizq"
Output for 7.0.5
string(32) "x9LaFAQObHKRVL5NZiKElxZJT0P9dM9x" string(8) "efaipnvm"
Output for 7.0.4
string(32) "6C29WR5paen90cmb5eL3HecmZHTpLl0O" string(8) "ycgxvvos"
Output for 7.0.3
string(32) "cOD3wjunmPAEYMVX5VyupbocUV6T5CvG" string(8) "nhezhpxa"
Output for 7.0.2
string(32) "uNvtU8vjDKpQxrA0aCXdLo8NzNwG5hlg" string(8) "pbmqetfw"
Output for 7.0.1
string(32) "jl3QoFi3Wew8IpJG4QSYZwURJWkstrmp" string(8) "wtdranbb"
Output for 7.0.0
string(32) "794b0K7322gJ66b9RaDdgTyLsnQuFmwl" string(8) "stwgefbe"
Output for hhvm-3.22.0
string(32) "HrXrxDPSGLbk8fmHgsur2PHti55zzo0p" string(8) "ivuyhwmz"
Output for hhvm-3.21.3
string(32) "sMYxVfQ8kgFSZyMDP9Iqu1E1v8SObWz2" string(8) "udsphccm"
Output for hhvm-3.21.0
string(32) "xx9XgmXcmJZXbdMNiB64gtr8U00syAtS" string(8) "kygfrinv"
Output for hhvm-3.20.2
string(32) "1viEcAluGwSuDixiUuOxVo4EBC4KsM5x" string(8) "dxuewcod"
Output for hhvm-3.19.2
string(32) "8LhZ4JqeRliZSHCs4wqQb9gf7vdF4DyD" string(8) "rxipudde"
Output for hhvm-3.18.5
string(32) "HggwTYC2CAyINmE0XNLJUlNam0Fm5Rm5" string(8) "npxonzjl"
Output for hhvm-3.18.4
string(32) "wbAjQDFX6J00tUFVTg83ZtmlqFbsmXix" string(8) "zebjfgwc"
Output for hhvm-3.17.3
string(32) "sJVi7DFGVGn8j6OEOXlCCW8WLK9ivXkf" string(8) "unlgmask"
Output for hhvm-3.15.4
string(32) "3Z54qF6LKSyUwvfAIWvUO1kb8UVjZqQl" string(8) "qqwrceui"
Output for hhvm-3.13.2
string(32) "4jntJKXg8tA4gz2se5b0Hx4LbLppcO9H" string(8) "ypwvgzei"
Output for hhvm-3.12.14
string(32) "OmBSgcPz2gumnpPNq5DsKxdhoNZpqJMy" string(8) "aqakqbyp"
Output for hhvm-3.11.1
string(32) "8FWymK8h9VlauLe2qn9q95HZfAsEjhU2" string(8) "rvnhmzqw"
Output for 5.0.0 - 5.6.30, hhvm-3.10.1
Fatal error: Call to undefined function random_int() in /in/KKlc3 on line 20
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: random_int() in /in/KKlc3 on line 20
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function: random_int() in /in/KKlc3 on line 20
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: random_int() in /in/KKlc3 on line 20