Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Question on random character generation code.

New Here ,
Jul 22, 2009 Jul 22, 2009

I have made this code to generate a random key for a registration system, I keep getting an error and can not seam to figure out why, if anyone can assist it would be great. Here is the code:

function random_char($string) {
$length = strlen($string);
$postition = mt_rand(0, $length-1);
return($string[$position]);
}
function random_string ($charset_string, $length) {
$return_string = "";
for ($x < $length $x++);

^This is line 23 here...

$return_string .= random_char($charset_string);
return($return_string);
}
mt_srand((double)microtime() * 1000000);

$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$random_key = random_string($charset, 30);

Here Is My Error:

Parse error:  syntax error, unexpected T_VARIABLE, expecting ';' in /home/jewuypq/public_html/budget/scripts/p_reg.php on line 23

TOPICS
Server side applications
489
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 22, 2009 Jul 22, 2009

bregent wrote:

You probably want something like:

for ($x=1 ;$x < $length; $x++)

Close. It should be this:

for ($x = 0; $x < $length; $x++) {

  // loop code goes here

}

The counter needs to start at 0. If it begins at 1 you need this:

for ($x = 1; $x <= $length; $x++) {

  // loop code goes here

}

The middle condition becomes "less than or equal to".

Translate
LEGEND ,
Jul 22, 2009 Jul 22, 2009

Well I don't know php but it seems obvious to me that you are missing a semicolon between your expression in the for loop

for ($x < $length $x++);

You probably want something like:

for ($x=1 ;$x < $length; $x++)

But I know there are many variants of the php 'for loop' and like I said, I don't use php.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2009 Jul 22, 2009
LATEST

bregent wrote:

You probably want something like:

for ($x=1 ;$x < $length; $x++)

Close. It should be this:

for ($x = 0; $x < $length; $x++) {

  // loop code goes here

}

The counter needs to start at 0. If it begins at 1 you need this:

for ($x = 1; $x <= $length; $x++) {

  // loop code goes here

}

The middle condition becomes "less than or equal to".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines