i created random charactors script. but got this error for some times refreshing. Uninitialized string offset: 61 in [COLOR="darkred"]my php file[/COLOR] on line 5 Code (markup): This is code <?php $length = 5; $characters = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[rand(0, strlen($characters))]; } echo $string; ?> Code (markup):
I think the problem is that rand can generate up to exact length of the alphabet string you have so let's say it chooses the max amount. in this case there are no 5 characters after it to choose from. so you need to change it to: rand(0, strlen($characters)-$length) PHP: you can also try this: <?php $len=5; $chars = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; $string=str_shuffle($chars); $string=substr($string, rand(0, strlen($chars)-$len), $len); echo $string; ?> PHP: