3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Generate a random string * * @link https://paragonie.com/b/JvICXzh_jhLyt4y3 * * @param int $length - How long should our random string be? * @param string $charset - A string of all possible characters to choose from * @return string */ function random_str($length = 32, $charset = 'abcdefghijklmnopqrstuvwxyz') { // Type checks: if (!is_numeric($length)) { throw new InvalidArgumentException( 'random_str - Argument 1 - expected an integer' ); } if (!is_string($charset)) { throw new InvalidArgumentException( 'random_str - Argument 2 - expected a string' ); } if ($length < 1) { // Just return an empty string. Any value < 1 is meaningless. return ''; } // Remove duplicate characters from $charset $split = str_split($charset); $charset = implode('', array_unique($split)); // This is the maximum index for all of the characters in the string $charset $charset_max = strlen($charset) - 1; if ($charset_max < 1) { // Avoid letting users do: random_str($int, 'a'); -> 'aaaaa...' throw new LogicException( 'random_str - Argument 2 - expected a string that contains at least 2 distinct characters' ); } // Now that we have good data, this is the meat of our function: $random_str = ''; for ($i = 0; $i < $length; ++$i) { $r = random_int(0, $charset_max); $random_str .= $charset[$r]; } return $random_str; } /* ------------------------------------------------------------------------ */ var_dump(random_str(10)); var_dump(random_str(16, '0123456789')); var_dump(random_str(8, '0123456789abcdef')); var_dump(random_str(24, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.'));
Output for 7.4.1
string(10) "oazkaenoiq" string(16) "9034943510643630" string(8) "bca7b5b6" string(24) "rYX0nhwK.kB1gdxIXBPIyvvd"
Output for 7.4.0
string(10) "zjcuhxaima" string(16) "5007028199304484" string(8) "7c0ab00c" string(24) "hQnTZAzmyFLkGWtRUxBTVV+D"
Output for 7.3.13
string(10) "zjrrcibjgk" string(16) "2810196415666982" string(8) "8eb4e866" string(24) "maZt3E1LSeoGdBHF6uDYIq0S"
Output for 7.3.12
string(10) "dkrgrpelvk" string(16) "8004191326022146" string(8) "247cd951" string(24) "g7JpLKa.E8HV6J4KeH3Vf9pX"
Output for 7.3.11
string(10) "hizdgffcgy" string(16) "9518767013787953" string(8) "0bf60a19" string(24) "jXLm7gNGFGSmJU7McTuoZz1V"
Output for 7.3.10
string(10) "cjoahyzwkz" string(16) "3790305539786177" string(8) "4792ae2a" string(24) "m19pxSz1KUG+TG0GIowbLs0p"
Output for 7.3.9
string(10) "vxxuzkodut" string(16) "7267548169892137" string(8) "193dcdf1" string(24) "MMcJ2alII9r.Pu14HAPMl5cx"
Output for 7.3.8
string(10) "eyuzgnulwe" string(16) "0056831287277575" string(8) "bd52fd9b" string(24) ".RWFrB11KShpHdQmYu7QBnkv"
Output for 7.3.7
string(10) "txlpyexglx" string(16) "0809800988025336" string(8) "8ddb9e86" string(24) "tRn42lLoQ4.PjUMf1b8JyxkZ"
Output for 7.3.6
string(10) "ujiicejaot" string(16) "3335802896284015" string(8) "02f43131" string(24) "1SjDEcjSsYudbkUZGAPVNYpY"
Output for 7.3.5
string(10) "tadhhnrfvo" string(16) "2703731158244994" string(8) "293a57d3" string(24) "y7n73G1YunGjRAx1XCrYdrAC"
Output for 7.3.4
string(10) "ceeoredarx" string(16) "6900121009008858" string(8) "5a4feda3" string(24) "HjcDH+i8v.hxuysIrsmNEUmx"
Output for 7.3.3
string(10) "bechrqfxnt" string(16) "3658196062948762" string(8) "d05e7c54" string(24) ".7Vze1+qxp.NDk6BCY1Eq5po"
Output for 7.3.2
string(10) "gatypwtiqx" string(16) "9984201281949819" string(8) "1a6bc41f" string(24) "ZrayYYU5kr5JSKSGnk4jw2Zi"
Output for 7.3.1
string(10) "vrbszcgakk" string(16) "1164744147441313" string(8) "42f484c8" string(24) "lVSgNY3GWFSaLm1oM3N786T3"
Output for 7.3.0
string(10) "pdkrqmwibx" string(16) "8982164363773885" string(8) "eb54a4a0" string(24) "zNIRbHY5k.CELdu+Y61W4brQ"
Output for 7.2.26
string(10) "ehnkzinaoi" string(16) "5677354188400323" string(8) "c98e8012" string(24) "oZMe1vuseObJIHGtvp.RqI+N"
Output for 7.2.25
string(10) "prgbjgqfcr" string(16) "4563923822818225" string(8) "9343f543" string(24) "8M9biqUA1Hj5YYLtM2LEcBSX"
Output for 7.2.24
string(10) "djnthynrvl" string(16) "5910964515431518" string(8) "0564013c" string(24) "XfPcN.cKv.s2Wo++1JCS4cHE"
Output for 7.2.23
string(10) "wluhspnksy" string(16) "1296984241624006" string(8) "9baa16c1" string(24) "xToh1+C6GI1J4+XLSSR3dGHb"
Output for 7.2.22
string(10) "dbyoxwmzev" string(16) "8953877109581129" string(8) "dc0ce018" string(24) "mSLoI+2NBHkvYlrgKlOsk3DZ"
Output for 7.2.21
string(10) "tvafrojapn" string(16) "7913934363390513" string(8) "5aca7832" string(24) ".ItY0VMcJ3o35.2D5KXE5hLv"
Output for 7.2.20
string(10) "btfnirdhsn" string(16) "9831236607310795" string(8) "d0cb6df6" string(24) "HFLFGI5jhKRTluR2gAccwK5s"
Output for 7.2.19
string(10) "ssiumhrejc" string(16) "0936285093791790" string(8) "0c1469e1" string(24) "UwJcwy.zBOG0AvHwwgHdSq23"
Output for 7.2.18
string(10) "oduftufovt" string(16) "4749634566844965" string(8) "e2e00eee" string(24) "q.bGoBANojuy4zQiHf0Z1YlJ"
Output for 7.2.17
string(10) "qqcrmkefun" string(16) "5828517191780198" string(8) "7a812902" string(24) "NljgHkfwrsO6JLY+Gy8pB8Xu"
Output for 7.2.16
string(10) "mwukdrkjcb" string(16) "3883377484183074" string(8) "9fd76908" string(24) "i5QRQzpmuPGe7sWQGuZorgYH"
Output for 7.2.15
string(10) "mlzoxoekcx" string(16) "9999171992012912" string(8) "a443cbab" string(24) "jm771hhaB3difML4.HIvxrU9"
Output for 7.2.14
string(10) "wdvtdjryxc" string(16) "6095154976230221" string(8) "2bbd6e5b" string(24) "sTTjtvXrsE7KVObvugH3+9Vx"
Output for 7.2.13
string(10) "hqdfmotvzo" string(16) "4549425099590310" string(8) "8f1a7719" string(24) "AagFtYlhf1WBqdK4P3RrbVY6"
Output for 7.2.12
string(10) "wdfmoqwlzp" string(16) "3781317477493551" string(8) "5022bda6" string(24) "AoNnvsFMxPYtpe19VXSY4ygf"
Output for 7.2.11
string(10) "hvrynrzcvo" string(16) "7447938613689925" string(8) "bd172b44" string(24) "RP2w7lVSSAYYoTTbF9dbp7WG"
Output for 7.2.10
string(10) "woxuwfcxrp" string(16) "4120892895772602" string(8) "169a8c38" string(24) "b7H8x943.e.y596oyIsUmMta"
Output for 7.2.9
string(10) "kmdfedlhnx" string(16) "8330180237472513" string(8) "6b086cb1" string(24) "Jy0wwwUiuMaFoc+7zHkOS0RM"
Output for 7.2.8
string(10) "rdqrgjteot" string(16) "1777081470785515" string(8) "9945150d" string(24) ".s6W.1HBgTyfTsyz2cYL+1oQ"
Output for 7.2.7
string(10) "zisvorhsqm" string(16) "6647625069372893" string(8) "fe82eaf7" string(24) "YbKuDOV.f5XezLwdNIM5j.HE"
Output for 7.2.6
string(10) "vvivlnpjrb" string(16) "4120796363129250" string(8) "1681faf6" string(24) "3DPc8q3OV8qcq8D3dWre.0.Q"
Output for 7.2.5
string(10) "zvhbqoddfg" string(16) "2274455444594146" string(8) "52592c75" string(24) "MCgZYZxz9L9oJfyZOSbzkPhr"
Output for 7.2.4
string(10) "rmuvwckdge" string(16) "3515290305884771" string(8) "8f492a6a" string(24) "VikodvVRpX3kNxMAfUooz0e6"
Output for 7.2.3
string(10) "iojuydirfr" string(16) "6941802761069889" string(8) "d0e87e63" string(24) "mrjaNnv+YlwLq5jyzkZ4aBOq"
Output for 7.2.2
string(10) "ucbydcbtft" string(16) "1268138687444715" string(8) "8f93b19e" string(24) "YdXoMk8NYN.XGWzR4ETcvWxu"
Output for 7.2.1
string(10) "egnddqcnqe" string(16) "2354122349909484" string(8) "93c6a464" string(24) "KbcE6i+GZiBf.lRHp3lR9leh"
Output for 7.2.0
string(10) "adyoykonkm" string(16) "3754118031554073" string(8) "6bc07e8a" string(24) "ONGVQuXYGISo9cJ9dkr8bjiJ"
Output for 7.1.33
string(10) "gyfhpuplhj" string(16) "1911451346613982" string(8) "87ec878d" string(24) "JJTqGiEeklgS8wx8zJ6VNKiQ"
Output for 7.1.32
string(10) "rerhqpcepr" string(16) "3115297950902148" string(8) "c709f1ee" string(24) "Mn8MgTlALhxe3t08Y.X7Vurv"
Output for 7.1.31
string(10) "rflmnyazti" string(16) "9312143670901742" string(8) "cad6c087" string(24) ".ze+fJWeHmNETPWy2XBnKLDT"
Output for 7.1.30
string(10) "cnqzzwpuhr" string(16) "6320647417484346" string(8) "7d2e07b8" string(24) "5jofo718h0Slb4JWxSVmrqUb"
Output for 7.1.29
string(10) "rtspszlxeq" string(16) "7639402758395749" string(8) "669d083d" string(24) "8V+o1XmcASsckZAQkFar2Qdi"
Output for 7.1.28
string(10) "lpbkpykhlq" string(16) "0771352571355483" string(8) "b1b309ae" string(24) "IbBgfyfRIb1PUt2SmEXF0Ir+"
Output for 7.1.27
string(10) "paogcdzfus" string(16) "1658034575337308" string(8) "57ebaf3c" string(24) "4DGkrZIAlHgo3a2zBcKFMzNw"
Output for 7.1.26
string(10) "hprupbsqbr" string(16) "0555177355109626" string(8) "ae8aada5" string(24) "iBXB6sKP6j7Pkt+2jY6FiqdE"
Output for 7.1.25
string(10) "soybeinxci" string(16) "4214862545739531" string(8) "524522fe" string(24) "JGMDAqe6+45XJdLQwQ6lcyhS"
Output for 7.1.24
string(10) "lsmjbdywyv" string(16) "9266048357049856" string(8) "f0734e30" string(24) "kbZrdxL5PriiF.pNqXTwTlFL"
Output for 7.1.23
string(10) "gjiejgfhaf" string(16) "1991295462759168" string(8) "1ee869f4" string(24) "Qc.z7lmSjdovel7npVwWqncP"
Output for 7.1.22
string(10) "divbprbzpg" string(16) "6011831632585238" string(8) "7d3c0146" string(24) "uo8Y0MSVcuDiF.8cSasjo7pH"
Output for 7.1.21
string(10) "gzuvzausub" string(16) "0824626558221094" string(8) "e0d4751c" string(24) "oaXiaz8OxnOcBWA3GKOYLlIV"
Output for 7.1.20
string(10) "ltsdmdfcrj" string(16) "4190893716689224" string(8) "ecaa56c0" string(24) "o2i7kU6Ty8kcnOCjQThebGn1"
Output for 7.1.19
string(10) "uzomlbhaqc" string(16) "7924449380896886" string(8) "fa2d9415" string(24) "2RrJGXUQoVsl.vuoCfSd5GJ9"
Output for 7.1.18
string(10) "hbmudtgvxv" string(16) "9459418376001727" string(8) "a430e15f" string(24) "Rt+3K8imlDuU3uy59ELFwHTO"
Output for 7.1.17
string(10) "wdcflordjj" string(16) "1831561015986542" string(8) "76b4a3b7" string(24) "A9uOJd266+wd529x3VYPrNE3"
Output for 7.1.16
string(10) "xdzwzqtytr" string(16) "8644682901319140" string(8) "3106cbb9" string(24) "YNBe5CWUeT2mCwMilOiugnJ3"
Output for 7.1.15
string(10) "eclzdinhbe" string(16) "0192497822686438" string(8) "f4b4b7cd" string(24) "s1z.adYM.HSUpHo62ERjBb3I"
Output for 7.1.14
string(10) "wgydckywiq" string(16) "0289518673934232" string(8) "95589668" string(24) "xdBQ5mJqZktAmAQeGN0UgDwM"
Output for 7.1.13
string(10) "nlzejhbfws" string(16) "3874376639574001" string(8) "d11c2e6c" string(24) "4gPWKqI+TsXiyj5WxT9zeorA"
Output for 7.1.12
string(10) "inrdysndge" string(16) "7714496964881032" string(8) "0e6f3a5d" string(24) "TqEW0I+FC+n2wO..0BcLSQ4Z"
Output for 7.1.11
string(10) "pzdudwjghy" string(16) "5561543855191680" string(8) "dfd379c9" string(24) "6+CZLD50OfJYAG0+Nny0DD00"
Output for 7.1.10
string(10) "gkegmodvid" string(16) "7450697525336506" string(8) "2ed99026" string(24) "dDeC6ZVrgduQPPf4TYp2Rx2x"
Output for 7.1.9
string(10) "qequomtill" string(16) "8619111186442404" string(8) "528ae73d" string(24) "p22A6BEhBOjYJK9OW5nZ5C9L"
Output for 7.1.8
string(10) "aujeczoyat" string(16) "6263980057613181" string(8) "044a24b9" string(24) "pc4814KL2xSYTQlM2.2Uy5zS"
Output for 7.1.7
string(10) "ayxxnbefoa" string(16) "0985214243045634" string(8) "7d627684" string(24) "4SnK2ndvbNi9vuCvTTnjM45z"
Output for 7.1.6
string(10) "firfpqdhih" string(16) "0313430545721134" string(8) "dcac22d7" string(24) "YCXxXQNGRNjhZqlkTy83kFQR"
Output for 7.1.5
string(10) "reynpfbtrb" string(16) "4139111840714831" string(8) "60c072eb" string(24) "SBUbvBlthVCFTihFyLKI32kV"
Output for 7.1.4
string(10) "zsasrghsoo" string(16) "9870738660471634" string(8) "efc589aa" string(24) "7rn03X3G2FDdCcC2hEsi5LlH"
Output for 7.1.3
string(10) "qmjrnqabzr" string(16) "7247228790635726" string(8) "77d676c0" string(24) "RAfZCzGR1R9w3okAjiw4Q7vd"
Output for 7.1.2
string(10) "hgsvwgvipg" string(16) "1871497778032393" string(8) "30dd91e6" string(24) "ZMK65V12nX3K06gRskqLD+Cj"
Output for 7.1.1
string(10) "zyanccwjwl" string(16) "0622076565337020" string(8) "ad1f8273" string(24) "RM+K9ekW6Fg+McnWOmmPSvHq"
Output for 7.1.0
string(10) "tdwrzwjbrf" string(16) "6852029124270552" string(8) "667eec41" string(24) "bBk0h6GFW.Njp86bcU0csTVh"
Output for 7.0.33
string(10) "unigutwawd" string(16) "1035492443744547" string(8) "b9d67385" string(24) "6HmXcAhhKPi1XGcEz2qKkB6r"
Output for 7.0.32
string(10) "aedseymhtr" string(16) "2013133145203089" string(8) "69e0ea57" string(24) "qZCTHxf21hYxS5xb2PhObvoJ"
Output for 7.0.31
string(10) "aqpdcyaohh" string(16) "5314613481449880" string(8) "0f227ab7" string(24) "d78edwVzqwef13YNOQ12GxYP"
Output for 7.0.30
string(10) "qkdkpcmhtw" string(16) "7519640002091468" string(8) "1371f122" string(24) "oVPDuv056WDyUGgi3.EOQ7yE"
Output for 7.0.29
string(10) "aklsbbkqud" string(16) "7151218876305852" string(8) "d0290c39" string(24) "88tYwIVJKRcvkVfO56THh.VI"
Output for 7.0.28
string(10) "snlheanimw" string(16) "9210575030393150" string(8) "8802f1ff" string(24) "hBdq6CT7QST.RsAYwXNYamdX"
Output for 7.0.27
string(10) "uujvnezapr" string(16) "0995075281578984" string(8) "4fb3718c" string(24) "QUUThd2p133Hisg+Ze0+vL.R"
Output for 7.0.26
string(10) "gpqcoqcxib" string(16) "2139307101278598" string(8) "aa053cf2" string(24) "Ml8oKyAjSbc.2uZ64s6zfdZl"
Output for 7.0.25
string(10) "cuyvoizkon" string(16) "3539029606627208" string(8) "e25a6db6" string(24) "RX6jEBw4pmdeLkZk.9xsifmW"
Output for 7.0.24
string(10) "ybzonbuhje" string(16) "5939372990899496" string(8) "60f9560e" string(24) "qQw9gcZTayx7bOMWkLq4D5LD"
Output for 7.0.23
string(10) "evcopgblic" string(16) "8424079453191727" string(8) "2b7beaa3" string(24) "0BDr5tpF+MBISUvBn134t31I"
Output for 7.0.22
string(10) "hgtpspcyqz" string(16) "4367433555375600" string(8) "3fcb0698" string(24) "3F404hq709OJ6cFZNp7d+334"
Output for 7.0.21
string(10) "xwrcnnsyjp" string(16) "8924029190254699" string(8) "a24919d4" string(24) "8vjcOo0HH5js7jA3jek0eSZd"
Output for 7.0.20
string(10) "vengnajpgq" string(16) "8102164427502444" string(8) "c3c72347" string(24) "YABExx3D6nQWdi2jZyKCObGW"
Output for 7.0.19
string(10) "mnzukuocvh" string(16) "0057159647624601" string(8) "d1c36c88" string(24) "dXXKRGUtg73mkeHhbboK5qKa"
Output for 7.0.18
string(10) "qkeaugxpcn" string(16) "7910226787513470" string(8) "cf1b0d8f" string(24) "v7.s86dJQJnAsrXIkVUtn10X"
Output for 7.0.17
string(10) "pvicixitek" string(16) "6582751520045006" string(8) "db1f854c" string(24) "5Xzfw9nHbDZgiDZ+2dIiZfM0"
Output for 7.0.16
string(10) "mogpoxigaw" string(16) "1229711572583100" string(8) "d4f4d5c5" string(24) "np++UU6TrUCuw9gM9HL2gTFv"
Output for 7.0.15
string(10) "lmdriyduhi" string(16) "1740463457933997" string(8) "c34aac75" string(24) "SDiR5AXiDf2LV1l45TPciUyj"
Output for 7.0.14
string(10) "lfghphycmt" string(16) "3873682004088991" string(8) "9154e8bb" string(24) "dP.+MVF2vPVAnITD4rvHv7tL"
Output for 7.0.13
string(10) "naeftelytv" string(16) "6245312194558019" string(8) "052d08b0" string(24) "b6lrQG7H2DGiro3EfleB4Bv3"
Output for 7.0.12
string(10) "loedsmcdom" string(16) "5676499919167088" string(8) "efc16971" string(24) "Yg1Ih3O94eJoAgG6vnb227Ls"
Output for 7.0.11
string(10) "pwhlilatnr" string(16) "7742784775561949" string(8) "86666974" string(24) "B4RYfuQjbtRAqO++pasfs+kw"
Output for 7.0.10
string(10) "zqfjrpxndr" string(16) "7754504404643914" string(8) "82dc641c" string(24) "H.yeq1CHZQdea8wz9HCw2fs1"
Output for 7.0.9
string(10) "bowgbegqsi" string(16) "6018935758891629" string(8) "8b5c3fe3" string(24) "jrrWOhxE22OsWAeiYyc9U0MN"
Output for 7.0.8
string(10) "jqiryoyzmj" string(16) "5719976925842066" string(8) "d2f1c83e" string(24) "1+oprvAd.09gc5jRw68ucHll"
Output for 7.0.7
string(10) "sdmwirdbra" string(16) "4218754659347469" string(8) "0b23fcf5" string(24) "fyR5v8kKnmVmw.3Ywnt9PMJB"
Output for 7.0.6
string(10) "jfdhqrrelp" string(16) "3809376098921350" string(8) "d9e1116c" string(24) "JjSmdP2ASJSHjqu0zhJFrNip"
Output for 7.0.5
string(10) "cgnpvgbfme" string(16) "3369872049780602" string(8) "7dab3eda" string(24) "my.z6hp7pOtR0hFl.yOo4qZe"
Output for 7.0.4
string(10) "cyfaakeowu" string(16) "9696340346855262" string(8) "d8736519" string(24) "taXtpSBBAugXYej6tRihvf1y"
Output for 7.0.3
string(10) "ysqylupone" string(16) "8528750217255601" string(8) "15341821" string(24) "v3f7FWBLZoTudlW5zXAszE72"
Output for 7.0.2
string(10) "utayeswghn" string(16) "0906835535527569" string(8) "e816a4fd" string(24) "QyqL.aSRSMu9KrLGSWVJ05xa"
Output for 7.0.1
string(10) "zbawkfabwk" string(16) "1281658031644756" string(8) "07567923" string(24) "ByP6ZCJB9kSXnKkEeh1CsCTB"
Output for 7.0.0
string(10) "auegflguip" string(16) "6084018753536688" string(8) "826a9143" string(24) "a6sBOOCJVoHteg+uK6XqeI49"
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Call to undefined function random_int() in /in/hd3hL on line 45
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/hd3hL on line 15
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_NEW in /in/hd3hL on line 15
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/hd3hL on line 15
Process exited with code 255.

preferences:
68.63 ms | 580 KiB | 5 Q