I want to generate a unique filename for uploaded files. They should be 5 characters in length, and have the following characters only: abcdefghijklmnopqrstuvwxyz0123456789. This is my code:
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$length = 5;
$filename = '';
for($i = 0; $i < $length; $i++)
{
$filename += $chars[mt_rand(0, 36)];
}
echo $filename;
But I always end up with 1 or 2 character long integers, never any string characters. If I run this code:
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$length = 5;
$filename = '';
for($i = 0; $i < $length; $i++)
{
echo $chars[mt_rand(0, 36)];
}
it works fine, and I get the following output: 8iwzf.
What could I be doing wrong here? Thanks!
You are adding (+=). You want to concatenate a string using dot.
$filename .= $chars[mt_rand(0, 36)];