harkly Posted May 19, 2010 Share Posted May 19, 2010 I have this code that generates a random string of numbers & letters. It works just fine but I need help with taking the variable $newPassword out of the for loop. I ultimately want to be able to email the $newPassword. Code that works ... <?php for ($i=0; $i<7; $i++) { $d=rand(1,9)%2; $newPassword= $d ? chr(rand(65,90)) : chr(rand(48,57)); echo $newPassword; } ?> Would like to be able to echo it outside of the for loop, right now it just echos 1 number or letter.... <?php for ($i=0; $i<7; $i++) { $d=rand(1,9)%2; $newPassword= $d ? chr(rand(65,90)) : chr(rand(48,57)); } echo $newPassword; ?> Link to comment https://forums.phpfreaks.com/topic/202290-for-loop/ Share on other sites More sharing options...
gwolgamott Posted May 19, 2010 Share Posted May 19, 2010 something like this: <?php for ($i=0; $i<7; $i++) { $d=rand(1,9)%2; $newPassword= $newPassword.$d ? chr(rand(65,90)) : chr(rand(48,57)); // OR us .= like so.... $newPassword.= $d ? chr(rand(65,90)) : chr(rand(48,57)); } ?> Link to comment https://forums.phpfreaks.com/topic/202290-for-loop/#findComment-1060734 Share on other sites More sharing options...
kenrbnsn Posted May 19, 2010 Share Posted May 19, 2010 How about something like: <?php $numslets = array_merge(range(0,9),range('a','z'),range('A','Z')); shuffle($numslets); $newPassword = implode('',array_slice($numslets,0,7)); echo $newPassword; ?> Ken Link to comment https://forums.phpfreaks.com/topic/202290-for-loop/#findComment-1060738 Share on other sites More sharing options...
harkly Posted May 19, 2010 Author Share Posted May 19, 2010 kenrbnsn your idea works great! I didn't think I really needed a for loop Thanks! Link to comment https://forums.phpfreaks.com/topic/202290-for-loop/#findComment-1060754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.