DEV Community

loading...

A function in PHP for generate Ramdom strings

Carlos Alberto
programming the world
・1 min read

If you need a function for generate ramdom strings, you can use this small fraction of code:

 <?php
<?php
    function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
?>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)