Copy link to clipboard
Copied
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
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".
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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".