knobby2k Posted June 16, 2011 Share Posted June 16, 2011 Hi guys, I have a piece of code that generates a random 10 character string but occasionally kicks up an error, please see the example code below... function genRandomString() { $length = 10; $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"; $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string; } Occasionally produces the error at the top of the page... Notice: Uninitialized string offset: 61 in /websites/... and points to line 27 of my code which is the " $string .= $characters[mt_rand(0, strlen($characters))]; " line of the code in the example above. Is my code incorrect? Why would it produce an error sometimes but work perfectly fine others?? Thanks Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/ Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 $characters is not an array, but you are trying to use it like an array. Try this instead: $characters = array_merge(range(0,9),range('a','z'),range('A','Z')); EDIT: You'd need another small modification too: Change: $string .= $characters[mt_rand(0, strlen($characters))]; To: $string .= $characters[mt_rand(0, count($characters)-1)]; Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/#findComment-1230559 Share on other sites More sharing options...
knobby2k Posted June 16, 2011 Author Share Posted June 16, 2011 lol, cheers redixx! Think you've helped me on every one of my questions so far since I joined!! Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/#findComment-1230563 Share on other sites More sharing options...
knobby2k Posted June 16, 2011 Author Share Posted June 16, 2011 Since changing the code it hasn't happened again so i'll mark this as solved. Thanks again mate Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/#findComment-1230573 Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 as further information, your script was not working because you were trying to address a string as an array. When using the array syntax on a string, it breaks down each character of a string as values...example $string = "Hello World"; echo $string[0];//outputs H echo $string[3];//outputs l etc. What was happening were there were times when your script would produce use the maximun strlen of your $characters var, so you would have 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY[61]; which would put your pointer one character after the full string, which cant be done, thus the error is triggered for invalid offset Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/#findComment-1230579 Share on other sites More sharing options...
knobby2k Posted June 16, 2011 Author Share Posted June 16, 2011 as further information, your script was not working because you were trying to address a string as an array. When using the array syntax on a string, it breaks down each character of a string as values...example $string = "Hello World"; echo $string[0];//outputs H echo $string[3];//outputs l etc. What was happening were there were times when your script would produce use the maximun strlen of your $characters var, so you would have 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY[61]; which would put your pointer one character after the full string, which cant be done, thus the error is triggered for invalid offset thanks for the explanation, makes perfect sense now!! Cheers guys Link to comment https://forums.phpfreaks.com/topic/239557-generating-random-string/#findComment-1230614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.