Patrick3002 Posted February 27, 2007 Share Posted February 27, 2007 Hi, i have a script that is supposed to created a random 10 digit number... It was working, then i changed something and now it doesn't work. I can't remember what i changed or how to get it to work now...rediculous. <?php $pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for($i=0;$i<10;$i++) { $rndpass = $pattern{rand(0,35)}; } echo $rndpass; ?> If anyone can help get it going again that would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/ Share on other sites More sharing options...
obsidian Posted February 27, 2007 Share Posted February 27, 2007 You're overwriting your $rndpass variable with each loop. You need to be adding to it: <?php $rndpass = ''; $pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for($i=0;$i<10;$i++) { $rndpass .= $pattern{rand(0,35)}; } echo $rndpass; ?> Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195414 Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 You can either treat your string as an array: $rndpass = $pattern[rand(0,35)]; or as a string $rndpass = substr($pattern, rand(0,35), 1); Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195416 Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 exactly what artacus said. Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195426 Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 Except with the dot like Obsidian said. Actually I tried the $pattern{n}, and it worked. But its not a syntax I'm familiar with. Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195445 Share on other sites More sharing options...
Orio Posted February 27, 2007 Share Posted February 27, 2007 The manual is a bit confusing about this issue. In the substr() function documentation it says single characters can be accessed by using curly braces ({}). In the part about strings in the manual, it says using square brackets (like in arrays) is recommended, and that the use of curly braces will be deprecated as of PHP 6. But they both work Orio. Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195450 Share on other sites More sharing options...
boo_lolly Posted February 27, 2007 Share Posted February 27, 2007 The manual is a bit confusing about this issue. In the substr() function documentation it says single characters can be accessed by using curly braces ({}). In the part about strings in the manual, it says using square brackets (like in arrays) is recommended, and that the use of curly braces will be deprecated as of PHP 6. But they both work Orio. VERY good to know. no wonder they call you orio. Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195477 Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 no wonder they call you orio Narf! I think so Brain, but wont they get angry if we dunk him in milk, crack him in half and eat the creamy center? Link to comment https://forums.phpfreaks.com/topic/40388-rand-and-for-problems/#findComment-195485 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.