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') { $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'));
Output for 7.2.4
string(32) "mPUklTK8htBLRRgqSUmKBuKTNVh2crHj" string(8) "gxxkurba"
Output for 7.2.3
string(32) "RluoByRN9MYAIlyyk1HKWbrsTr9d3Ah0" string(8) "rewtnmig"
Output for 7.2.2
string(32) "zmlib1x5L3irjmi8UMgs0uaciAb9jx3q" string(8) "pbwwrddq"
Output for 7.2.1
string(32) "ggLglxgManUYtzAF86Y1wAaiXuAiVpQE" string(8) "zbqecweb"
Output for 7.2.0
string(32) "g55IOz8j7cWWDKMAreHtLAFUCwmrko7H" string(8) "qsoytgvm"
Output for 7.1.16
string(32) "bqQNKOBuMCvOxhd32kCYEBHTjvnDbg1S" string(8) "fezbmctd"
Output for 7.1.15
string(32) "ARgwRm9XnWFjyD7rY1UwkletAxoUnXwH" string(8) "yqjrsgww"
Output for 7.1.14
string(32) "GOhADpREYmGi4fjSed6BzFfHFPf4K0Tv" string(8) "volnwqzg"
Output for 7.1.13
string(32) "18Om5coqwKq4msDN4GKZ8oGfzTh9dmzr" string(8) "eycqgoxd"
Output for 7.1.12
string(32) "SU0voGNvLBXah3FFxFZlkaMERgyeY9lJ" string(8) "utiucxrg"
Output for 7.1.11
string(32) "4RgSrCzbbGM7RwxzMRzyvPgyGEWJJweN" string(8) "bzyqcjfc"
Output for 7.1.10
string(32) "dij5SNAeUXIsn7IoJ4e6lT5ygFf3grWG" string(8) "gemugzja"
Output for 7.1.9
string(32) "zu1Hy2mGDLMLLt9D9rtBNNieQuLi68sQ" string(8) "eiwpztkr"
Output for 7.1.8
string(32) "r6W9jK0dXaRoeVfcGxWpSYTunU5WIlrb" string(8) "mdytykdt"
Output for 7.1.7
string(32) "dOsUYwxtJ1PQjfMghu07wevK7C4y9XM6" string(8) "yjmttnst"
Output for 7.1.6
string(32) "cFAWnkfY6ty6kAAXd4acG7iIEPsNrgub" string(8) "mpnegvsg"
Output for 7.1.5
string(32) "Y3gGGHnWKDoHfQA9E3Zs2xnmR8ttObwq" string(8) "cacyjako"
Output for 7.1.4
string(32) "PY7pRn9NsODOerBiR0DPvcirr0gTjaGZ" string(8) "rntrxhqw"
Output for 7.1.3
string(32) "YPkrQKlKTOS1nL6Zwn9EJ0IfpMMcOppJ" string(8) "ihivsxdg"
Output for 7.1.2
string(32) "sfDEvSGxszI8W2djeRlqHoo22DjF1GOE" string(8) "pccukwpd"
Output for 7.1.1
string(32) "5CRj4QyZP4r22hWvbQ0qnJLCm2mIPNvS" string(8) "iyicqxeb"
Output for 7.1.0
string(32) "3BOQu0DWo8fEiqP8G0IP2Wbtd5axxzXb" string(8) "sljrwnhf"
Output for 7.0.29
string(32) "skAgYMdtodwSzeGKmOCIERWf8iYdZPKg" string(8) "ecwakwij"
Output for 7.0.27
string(32) "LXepRIvl6PMOtz7EJq0ownZHhz8xbc0j" string(8) "tlysgkko"
Output for 7.0.26
string(32) "oXssZnowYYoIyKr76GegrmQgS4hTbXie" string(8) "vzipzrmx"
Output for 7.0.25
string(32) "h1L9eaIUhrL5v6DeHF6pLiOagNvWgoRI" string(8) "gfxbsgmc"
Output for 7.0.24
string(32) "6Bc10EVwRdxZt3UpWurwdgh0huobjbMb" string(8) "inbcooju"
Output for 7.0.23
string(32) "xyWqpa8yW8DmOikUJUuBkeqTkWSnfUd5" string(8) "ohoipjsk"
Output for 7.0.22
string(32) "YvNlL9TY31ct2RDYKQBQ9GiwyjgSQSiD" string(8) "ayjvoqql"
Output for 7.0.21
string(32) "QxRYlmVkiruglUdQpr94UJQaOqA6a0BZ" string(8) "xwbfpjrc"
Output for 7.0.20
string(32) "PCV10mK66WIZMsOmebKO5bf2GEQq2Cs6" string(8) "ogtylukt"
Output for 7.0.19
string(32) "LLtF0EU8yEH0VqpfxPpZYErkxTFAMtqm" string(8) "vwqilbav"
Output for 7.0.18
string(32) "4PQfhxg8V4ElMdXQVcYGHrH5GIbmShLg" string(8) "duilwqxk"
Output for 7.0.17
string(32) "IMtutsFXrWVj7abj1frVBrMYD1a5kfwu" string(8) "yoxtsepk"
Output for 7.0.16
string(32) "nkt9Wnys4Rwv1VdbbmRMf3tqIg62A280" string(8) "teepldyj"
Output for 7.0.15
string(32) "CqvcB3ONW7nv0GMQWKTKYMXIfb4jqSuP" string(8) "oqqsntpq"
Output for 7.0.14
string(32) "Lmjk6QTEJbX3cCWpZ8Nl1uxNA1chqf9g" string(8) "bdqvsnrb"
Output for 7.0.13
string(32) "ypM3dRHwXP7F5TMfyz2D3Kh2nCMry5IH" string(8) "rolaifkr"
Output for 7.0.12
string(32) "GFUFVGC7UKbMa8sapIhUR7FiYFXscSZy" string(8) "mvbkhnib"
Output for 7.0.11
string(32) "r6MoM1knvHRO6LlwqNe5jAp0r1zfPWhE" string(8) "kalfkoby"
Output for 7.0.10
string(32) "GXu84xjOINJ8QR4SyO53uXGEIV64IwkR" string(8) "rvztsynm"
Output for 7.0.9
string(32) "bf5KBKHHEsr9xRE35Em17opXO18s6eU7" string(8) "jwmerldt"
Output for 7.0.8
string(32) "y0sr0GmpLIFpmrmFt6KpgwAOyTHdQK94" string(8) "qcxhytda"
Output for 7.0.7
string(32) "cRXyDs2yp6NC7kLCMb1jFhe3J6gAub1g" string(8) "tluafjzr"
Output for 7.0.6
string(32) "BSDQ2wqBcYzGdO2jqXdHKXmWbH5pNLsM" string(8) "qjtekkcg"
Output for 7.0.5
string(32) "YibqLrAhAwUAZrIIypRg7ntxHMJE5PZH" string(8) "copcnlfd"
Output for 7.0.4
string(32) "yRyJSVRFjptJq2iXqwWZB3QRgzGm7l7s" string(8) "uqplgcmj"
Output for 7.0.3
string(32) "N9o7M8SV1Kv85gm1NShQTso1dCCMAwcI" string(8) "aiwjgxga"
Output for 7.0.2
string(32) "c0p40yp5otHebQ4i1E49gZfWMIe5pj3U" string(8) "tczilxux"
Output for 7.0.1
string(32) "D9rNggy68jI1PY1eaXODDArrlIRSohYB" string(8) "zyxntokm"
Output for 7.0.0
string(32) "ZwW3TUlt1FMzs1sb9gF837dfgnKFFZ5K" string(8) "dbcqeybu"
Output for hhvm-3.22.0
string(32) "INOkzH4RyFO2zWiHHB1fqHhREgvM2PMR" string(8) "cqcaqhgw"
Output for hhvm-3.21.3
string(32) "41Kaz05v6uQn4vjMx5kFGBVo7FMFTnCD" string(8) "xjwvzqdz"
Output for hhvm-3.21.0
string(32) "EqlvBvZN3HwnceWtp7NPAuaF1ptfp5f3" string(8) "edbpsnkh"
Output for hhvm-3.20.2
string(32) "N7GLt3rhSng0ne2NhPmZu60RaBxurtqA" string(8) "uolseybe"
Output for hhvm-3.19.2
string(32) "gmP5gXU9KBU9Pgo2w2U9Zn8GYrxiZ8WT" string(8) "fffugnqm"
Output for hhvm-3.18.5
string(32) "qipm1ppo63GQoNIDyakhYny1wKT07cnw" string(8) "aawelqni"
Output for hhvm-3.18.4
string(32) "W92YGQQJ5urMyfBDF7bX9CX8vDkGZNBn" string(8) "jsrzzsor"
Output for hhvm-3.17.3
string(32) "z7H54LQ59hQWEXgkpObUrdKjQRfXPYrB" string(8) "zqcqlxip"
Output for hhvm-3.15.4
string(32) "p5jObn1c4EAhv6BjwUlZesz8nvAogOgz" string(8) "sthoieoa"
Output for hhvm-3.13.2
string(32) "p1GAjYoMxhMnsGxg9fShhay8W3QxugA7" string(8) "iwzztxxg"
Output for hhvm-3.12.14
string(32) "C3hkehwOAJdrlCTj9Fo9JXyIIvMzVz4V" string(8) "ieejzpuk"
Output for hhvm-3.11.1
string(32) "ri8N2kOiQETGB5VOSVDQyNqKbMwlK7d5" string(8) "srkdzaps"
Output for 5.4.0 - 5.6.30, hhvm-3.10.1
Fatal error: Call to undefined function random_int() in /in/b4PST on line 20
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/b4PST on line 17
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/b4PST on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/b4PST on line 17
Process exited with code 255.