PHP: Random string with numbers and letters

I thought that some of you might find it useful to learn how to generate a random string or a random number with PHP. I wrote a quick function to use PHP to generate random. See it below :

function genRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];

    return $string;

There is an easier way to do this with a single line of code but you won’t have as much control over the characters that you want to include/exclude from the random PHP string. Here it is:

$string = substr(md5(microtime()), rand(0,99), 10);


  1. By ?

  2. By JoeShmoe

  3. By Matt

  4. By Sarah

  5. By Ben Borja

  6. By shane

  7. By Neil

  8. By laittg

  9. By jaime

  10. By takuhi

  11. By kukunkz

  12. By sid

  13. By den

  14. By Alireza

  15. By Zach

  16. By Risingfalcon

  17. By priyanka

  18. By Aditya

  19. By Eric

  20. By Jack

  21. By ddart

  22. By Ali Rizwan

  23. By Lutashi

  24. By saumil

  25. By arif

  26. By nitin

  27. By Carlos

  28. By LH

  29. By Eben

  30. By psychotech

  31. By Nicholas Pickering

  32. By Nicholas Pickering

  33. By joschmo

  34. By Wale

  35. By Dhaval

  36. By tim

  37. By tim

  38. By Daniel Schaefer

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>