Possible to use a while loop within a loop?
-
03-07-2019 - |
Question
What I am trying to do is the following: I have an array of values, these values will eventually be used to generate a random unique string but that's a little later. First I would like to loop through all the values in the array (foreach loop) then I would like to limit this (while loop) Is this a correct method for doing this?
The below code does not work, can anybody see what I'm doing wrong?
<?php
$array = array(
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '!', '£', '$', '%',
'^', '&', '*', '(', ')', '_', '+', '{', '}'
);
$length_of_unique_key = 15;
$counter = 0;
foreach($array as $values)
{
$counter++;
while ($counter <= $length_of_unique_key)
{
}
}
?>
Solution
Shouldn't you be incrementing your counter within the while
loop, so it can exit?
OTHER TIPS
The best way to see what is wrong with a loop (or any other control structure) is just to run through it. Sometimes you can do it in your head; at other times, it might be helpful to insert trace points into your code.
In this case, I think if you simply run through the code in your head, you'll be able to pick up what's wrong with it. But for didactic purposes, I'll run through it here. First let's number each line of code:
$array = array(...); // line 1
$length = 15; // line 2
$counter = 0; // line 3
foreach($array as $values) // line 4
{
$counter++; // line 5
while ($counter <= $length) // line 6
{
// line 7
} // line 8
// line 9
} // line 10
Now let's run through it:
$array
is assigned a single dimensional array:
array(0 => '1', 1 => '2', 2 => '3', ...)
$length
is set to 15.$counter
is to set 0.- Begin
for loop
;$values
=$array[0]
= '1'. $counter
is incremented.$counter
= 1.- Begin
while loop
; check that$counter
(1) <=$length
(15). - Do nothing.
- Go back to line 6.
- Line 6: If
$counter
(1) <=$length
(15), continue loop. - Line 7: Do nothing.
- Line 8: Go back to line 6.
- Line 6:
$counter
(1) is still <=$length
(15), go into loop again. - Line 7: Do nothing.
- Line 8: Go back to line 6.
As you can see, you are stuck in an infinite loop because neither $counter
nor $length
change values. So the while
condition in line 6 always evaluates to true (1 <= 15).
All the code you've posted is legit, but clearly you have left something out and that's the part that's going to help answering this... Otherwise, your $counter stays constant during the while loop and it'll never exit
Why not do something like the below code, it generates a key with one loop. Hell, why not make a function for generating keys?
function keyval($length)
{
$length_of_unique_key = $length;
$array = array(
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '!', '£', '$', '%',
'^', '&', '*', '(', ')', '_', '+', '{', '}'
);
for($i=0;$i<$length_of_unique_key;$i++)
{
$key.=$array[rand(0,sizeof($array)-1)];
}
return $key;
}
echo keyval(15);