Showing posts with label Generate a random string in PHP. Show all posts
Showing posts with label Generate a random string in PHP. Show all posts

Friday, 25 September 2015

How to create random string in PHP


How to create random string in PHP

Sometimes people wants random name like image name, Personal name, book name, etc.... Here i give you short example of random string.

<?php

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

echo $randomString;
?>