Forum Moderators: coopster

Message Too Old, No Replies

Random numbers in php

         

scoobie

4:24 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Hi,

I found this code on this forum and it works but i don't know what the lines in bold mean.

$data = "AbcDE123GHIJK4LMN567QRSTU89VWXYZ";
$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
$data .= "0FGH45OP89";
$Random .= substr($data, (rand()%(strlen($data))), 1);

Can someone please explain them to me.

Thanks in advance,

Scoobie.

demnetia

5:17 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



The variable data is basically a string of different random characters ".=" is used to append more strings to $data.

Essentially, here is what $data looks like:
$data = "AbcDE123GHIJK4LMN567QRSTU89VWXYZaBCdefghijklmn123opq45rs67tuv89wxyz0FGH45OP89";

You can lookup the defintion of substr on php.net. It basically takes only a part of the string (hence, sub-string).

In this specific piece of code, I'm assuming it's inside a loop since it's appending one character at a time to $Random.

rand() geneartes a random number, and % is the modulous operator that returns the remainder of the division of rand() by strlen($data)

The purpose of (rand()%(strlen($data))) is to generate a random number which will be used as an offset to start cutting from in substr.