views:

540

answers:

4

Howdy Guys,

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)
       {

       }
    }

?>
+7  A: 

shouldn't you be incrementing your counter within the while loop? you know... so it can exit?

pyrochild
Thanks Zach, +1
Keith Donegan
btw, sorry if that came across as patronizing. (obviously someone thought it was; it got downvoted). that was not the intent.
pyrochild
Hey, no bother at all mate, I'm just here to learn, cheers.
Keith Donegan
A: 

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);
Sam152
Oh I will be creating a function later on, just fleshing it out at present, thanks
Keith Donegan
A: 

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

Scott Evernden
+3  A: 

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:

  1. $array is assigned a single dimensional array:
    array(0 => '1', 1 => '2', 2 => '3', ...)
  2. $length is set to 15.
  3. $counter is to set 0.
  4. Begin for loop; $values = $array[0] = '1'.
  5. $counter is incremented. $counter = 1.
  6. Begin while loop; check that $counter (1) <= $length (15).
  7. Do nothing.
  8. Go back to line 6.
  9. Line 6: If $counter (1) <= $length (15), continue loop.
  10. Line 7: Do nothing.
  11. Line 8: Go back to line 6.
  12. Line 6: $counter (1) is still <= $length (15), go into loop again.
  13. Line 7: Do nothing.
  14. 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).

Calvin
Exceptionally well explained Calvin, thank you
Keith Donegan