By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

Changing arrays at runtime

P: 105
Here is the code

[PHP]$alphabet = array("a","b","c");
$number_of_letters = 3; //initial amount of characters

foreach($alphabets as $letter)
print $letter;

if ($number_of_letters != 26)
//add another character to the $alphabet array
//AND increase the $number_of_letters by 1

Now here is the question :

The above code will only process the initial array values even thoug the array is being added to, ie it will print only a,b and c

Logically, it should carry on printing as the $aphabet array is being continiously chaged.

Any thoughts appreciated
Dec 14 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 5,821
Heya, Aktar.

In this case, you'll want to use a for loop, as the condition is evaluated at every iteration:

Expand|Select|Wrap|Line Numbers
  1. for( $__i = 0; $__i < $number_of_letters; ++$__i )
  2. {
  3.     .
  4.     .
  5.     .
  6. }
When you add a letter to the array, be sure to increment $number_of_letters. Once you stop adding letters, your script will stop incrementing $number_of_letters, and then your loop will (eventually) terminate.
Dec 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.