2

Hello i want to generate random string in php with repetition like Ag7hlA. is there anyway to do it i am trying this

<?php
function random_string($length)
{
    $length = (int) $length;
    if ($length < 1) return '';

    $base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
    $max = strlen($base) - 1;

    $string = '';    
    while ($len--)
    {
        $string = $base[mt_rand(0, $max)];
    }
    return $string;
}    
?>

But the random string is not being generated

1

1 Answer 1

0
function generateRandomString($length = 10) {
    $char = 'abcdefghijklmnopqrstuvwxyz';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $char[rand(0, strlen($char) - 1)];
    }
    return $randomString;
}

if u want add numbers too, than the $char will be--->

$char = 'abcdefghijklmnopqrstuvwxyz1234567890';

and if capital cases--->

$char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
Sign up to request clarification or add additional context in comments.

2 Comments

i tried it but it is not working
i tried above function and at end echo generateRandomString(); string generated is only one alphabet

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.