note 52720 added to function.rand

From: Date: Tue, 10 May 2005 21:30:45 +0000
Subject: note 52720 added to function.rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-89232@lists.php.net to get a copy of this message
There is one problem with Luis and Goochivasquez's code. substr starts at 0 not one, so the string is 0-39 not 1-40. This means that substr would return false sometimes and would give you a length smaller than expected. Here's a fix: // RANDOM KEY PARAMETERS $keychars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $length = 40; // RANDOM KEY GENERATOR $randkey = ""; $max=strlen($keychars)-1; for ($i=0;$i<$length;$i++) { $randkey .= substr($keychars, rand(0, $max), 1); } ---- Manual Page -- http://www.php.net/manual/en/function.rand.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52720 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52720&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52720&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52720&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52720&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52720&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52720&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89232) next »