adv Posted December 20, 2008 Share Posted December 20, 2008 hello i have this function to generate random letters and numbers with any length that u want <?php function randomPassword($length = { // all the chars we want to use $all = explode( " ", "a b c d e f g h i j k l m n o p q r s t u v w x y z " . "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z " . "0 1 2 3 4 5 6 7 8 9 _"); for($i=0;$i<$length;$i++) { srand((double)microtime()*1000000); $randy = rand(0, 61); $pass .= $all[$randy]; } return $pass; } echo $new_filename = randomPassword(15); ?> i didnt make this myself i found it from google but the thing that i dont understand is here $pass .= $all[$randy]; why does it need to concatentate ?? i tried without the "." but it will show only one random letter or number how is the concatentate operator makes such a big difference ::| any oppion is good thanks Quote Link to comment https://forums.phpfreaks.com/topic/137847-solved-quick-question-d/ Share on other sites More sharing options...
Mark Baker Posted December 20, 2008 Share Posted December 20, 2008 because when you populate $randy from the array, you're just setting it to a single character, and you concatenate all those single characters into $pass up to the length you want for your password Quote Link to comment https://forums.phpfreaks.com/topic/137847-solved-quick-question-d/#findComment-720427 Share on other sites More sharing options...
RussellReal Posted December 20, 2008 Share Posted December 20, 2008 .= is shorthand it is like: $var .= "lol"; $var = $var."lol"; ^^ both of those do exactly the same thing Quote Link to comment https://forums.phpfreaks.com/topic/137847-solved-quick-question-d/#findComment-720433 Share on other sites More sharing options...
.josh Posted December 20, 2008 Share Posted December 20, 2008 Unless you're using a really old version of php, there's no need to seed the random number generator for rand or shuffle or any of the other randomize type functions. function randomPassword($length = { $charlist = array_merge(range('a','z'), range('A','Z'), range(0,9), '_'); shuffle($charlist); return implode('', array_slice($charlist, 0, $length)); } // end randomPassword Quote Link to comment https://forums.phpfreaks.com/topic/137847-solved-quick-question-d/#findComment-720445 Share on other sites More sharing options...
adv Posted December 21, 2008 Author Share Posted December 21, 2008 thanks alot all for ur answers i see now Quote Link to comment https://forums.phpfreaks.com/topic/137847-solved-quick-question-d/#findComment-720557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.