0

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

          }
    }

?>
1
  • Using the code I posted, I generated a key of a million characters with no problemo. Commented May 3, 2009 at 0:55

4 Answers 4

11

Shouldn't you be incrementing your counter within the while loop, so it can exit?

Sign up to request clarification or add additional context in comments.

1 Comment

btw, sorry if that came across as patronizing. (obviously someone thought it was; it got downvoted). that was not the intent.
7

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

Comments

0

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

Comments

0

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

1 Comment

Oh I will be creating a function later on, just fleshing it out at present, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.