Jump to content

[SOLVED] getting value of variable from FOR STATEMENT


paparanch

Recommended Posts

good day gurus!

 

i have this code which generate 8 random alphanumeric:

 
srand(time());
$pool = array_merge(
range('a', 'z'),
range('A', 'Z'),
range(0, 9)
);
$pool_size = count($pool) - 1;
for ($i = 0; $i < 8; $i++) {
  $code = $pool[rand(0, $pool_size)]; <<<<< i want to get its value outside the for statement
}

 

how could i get the value of the variable $code outside the for statement?

because when i tried to echo the varibale after the for statement only the last digit of 8 alphanumeric will appear...

 

plss help..

 

 

 

 

that means your $code is echoing outside the for loop, but your calculation for getting the variables is wrong, if it was not echoing the variable then the result would have no date shown, as you are getting something indicates you previous code to get the value has an error

hi mr.phpdragon! i really dont get what you mean (im just noob in php).

 

this code works very fine:

 

for ($i = 0; $i < 8; $i++) {

  $code = $pool[rand(0, $pool_size)];

  echo $code; <<<(output "65KbldxB")

}

 

but when i echo outside the for statement i only get the last digit:

 

for ($i = 0; $i < 8; $i++) {

  $code = $pool[rand(0, $pool_size)];

}

echo $code; <<<(output "B")

 

 

but when i echo outside the for statement i only get the last digit:

 

Of course you do, because that is the last value stored within $code. Either echo it within the loop, or have each value concatinated onto the end of $code. eg;

 

for ($i = 0; $i < 8; $i++) {
  $code .= $pool[rand(0, $pool_size)];
}
echo $code;

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.