Results 1 to 3 of 3

Thread: Loop Puzzler

  1. #1
    Join Date
    Apr 2012
    Central New Jersey
    Thanked 3 Times in 3 Posts

    Default Loop Puzzler

    Hi Folks,

    Back again with a new problem. I think it's a simple one, but it escapes me:

    There are sixty-four buildings numbered building one through building sixty-four. I want to "visit" each of the buildings one time, selecting the building to be visited in random order, but I do not want to visit building six at all. The code accomplishes that seemingly perfectly, except that after reaching the loop limit, I'm echoing one last time, with the building number blank. Here's the code:

    PHP Code:
    $buildings range (164);
    $building_index 1;
    shuffle ($buildings);
    while (
    $building_index 65) {
        if (
    $buildings[$building_index] !== ){
    "Building " $buildings[$building_index] . "&nbsp;All hail Caesar!<br />";
    $building_index ++;

    Here are the last three lines of what the above code produces:

    Building 4 All hail Caesar!
    Building 56 All hail Caesar!
    Building All hail Caesar!

    What am I missing?

    Last edited by marain; 08-30-2018 at 11:38 PM.

  2. #2
    Join Date
    Jan 2015
    Thanked 19 Times in 19 Posts


    The $buildings array contains data with values 1 - 64, but has index values 0 - 63. The last index value of 64 doesn't exist. If you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting a php error about an undefined index value.

    After you shuffle() the $buildings array, I would just use a foreach(){} loop on that array to output the result. There's no need for any of the code for the $building_index variable. You could also just unset() the 5th index element before you shuffle the array to remove building #6 from the set of data.

  3. The Following User Says Thank You to DyDr For This Useful Post:

    marain (08-30-2018)

  4. #3
    Join Date
    Apr 2012
    Central New Jersey
    Thanked 3 Times in 3 Posts


    Of course! So elementary!

    Thank you.


Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 03:10 AM
  2. Replies: 8
    Last Post: 08-03-2008, 03:48 AM
  3. [help] WHILE loop or not?
    By csj16 in forum PHP
    Replies: 5
    Last Post: 07-19-2008, 04:06 PM
  4. for loop help
    By jamiller in forum JavaScript
    Replies: 6
    Last Post: 04-27-2007, 09:19 PM
  5. for Loop?
    By Medyman in forum Flash
    Replies: 1
    Last Post: 04-20-2007, 11:42 PM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts