what is does this code means

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fird01
Forum Newbie
Posts: 19
Joined: Mon Apr 07, 2008 11:04 pm

what is does this code means

Post by fird01 »

what does this coding means..?

Code: Select all

if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength); 
    else                         $length = mt_rand ($minlength, $maxlength); 
    for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    return $key;
especially this one

Code: Select all

$charset[(mt_rand(0,(strlen($charset)-1)))]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: what is does this code means

Post by Christopher »

Code: Select all

$length_of_string_charset = strlen($charset);
$index_of_last_character_in_charset = $length_of_string_charset - 1;
$random_index_in_charset = mt_rand(0, $index_of_last_character_in_charset);
$random_character_in_charset = $charset[$random_index_in_charset];
(#10850)
codeblock
Forum Newbie
Posts: 4
Joined: Sun Apr 27, 2008 5:26 pm
Location: USA

Re: what is does this code means

Post by codeblock »

fird01 wrote:what does this coding means..?

Code: Select all

if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength); 
    else                         $length = mt_rand ($minlength, $maxlength); 
    for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    return $key;
if $minlength is greater than $max length, then $lenth is a random variable between $maxlength and $minlength.

otherwise, $length is a random number between $minlength and $maxlength

especially this one

Code: Select all

$charset[(mt_rand(0,(strlen($charset)-1)))]
That takes everything in an array, and appends it to $key randomely.

and it returns $key
Post Reply