Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Generating random alphanumeric code

$
0
0

I am trying to generate a random 4 character alphanumeric code.  For some reason my script is sometimes generating a 3 character code, though most the time it is doing a 4 character.  Thanks for your help!

<?php
function randomCode() {
    $length = 4;
    $characters = '2345678ABCDEF';
    $code= '';
 
    for ($p = 0; $p < $length; $p++) {
        $code.= $characters[mt_rand(0, strlen($characters))];
    }
 
    return $code;
}
echo randomCode($code);
?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles