note 35652 added to function.rand

From: Date: Tue, 09 Sep 2003 22:42:19 +0000
Subject: note 35652 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-55905@lists.php.net to get a copy of this message
Most of the posts here are about generating passwords, and thats fine, but I wanted to point out something I saw as I glanced through a few of your functions... I'm using the function below as an example but I saw 3 others that had the same problem... the line in the for loop will generate an error because remember when your using a variable as an array 0 is the first entry so strlen($A) - 1 must be used in this case function GenPass($_maxlen) { $A = "abcdefghijklmnopqrstuvwxyz0123456789"; for ($i=0;$i<=$_maxlen;$i++) { $code .= $A[rand(0,strlen($A))]; // change this line to: // $code .= $a[rand(0,strlen($A)-1)]; } return $code; } ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35652 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35652&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35652&report=yes

« previous php.notes (#55905) next »