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)

DEV runs on 100% open source code known as Forem.

 
Contribute to the codebase or host your own.
 
Check these out! 👇