Brain Goo

Carpe Crap ’em

Brain Goo header image 2



HOWTO: Create a random alpha-numeric string in PHP

January 2nd, 2014 · No Comments

Creating an alpha-numeric string in PHP is easy, but having a quick function you can call is better!

This is very useful for things like password generators.

To see how to do this in Python, check out this post: …howto-create-a-random-alpha-numeric-string-in-python

To see how to do this in Perl check out this post: …howto-create-a-random-alpha-numeric-string-in-perl

function makeID($size = 6) {
    global $makeIDpool;
    $makeIDpool = array_merge(range(0,9), range('a','z'), range('A','Z'));
    $append = function ($value){ global $makeIDpool; return $makeIDpool[rand(0,count($makeIDpool) - 1)]; };
    return implode('', array_map($append, range(0,$size - 1)));


This code sample:

print makeID().'<br />';
print makeID(10).'<br />';
print makeID(20).'<br />';
print makeID(1).'<br />';

Will print something like this:


Did you find this post useful or have questions or comments? Please let me know!

Tags: How Tos · php · Programming

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment