DEV Community

Carlos Alberto
Carlos Alberto

Posted on

A function in PHP for generate Ramdom strings

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

Top comments (0)