Jump to content

rand and for problems


Patrick3002

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.