While loop in PHP: undefined offset
Results 1 to 2 of 2

Thread: While loop in PHP: undefined offset

  1. #1
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,429

    While loop in PHP: undefined offset

    I started studying PHP yesterday, and I'm facing a problem already...
    Here's my code:


    Code:
    <html>
    <head>
    <title>Test array</title>
    </head>
    <body>
    <?php
    // Here I define my array
    $assortiment = array("value1","value2","value3","value4","value5","value6");
    // Here I just want to display my array using a while loop
    echo "Arrays unsorted
    ";
    echo "Assortiment: ";
    $i=0;
    while($assortiment[$i]){
      echo $assortiment[$i] . " ";
      $i++;
    }
    ?>
    </body>
    </html>
    This works, but keeps on returning this:

    Arrays unsorted
    Assortiment: value1 value2 value3 value4 value5 value6
    Notice: Undefined offset: 6 in D:\ministry\testarray.php on line 13
    Line 13 is the 'while'-line...

    I tried Googling 'undefined offset', but all it returns are webpages facing the similar problem...

    Anything I'm doing wrong? I'm running PHP 4.2.3 on Apache (Windows).

  2. #2
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    ok as promised fixed code
    can't take credit tho - thank D`Amour (my housemate)

    Code:
    <html>
    <head>
    <title>Test array</title>
    </head>
    <body>
    <?php
    // Here I define my array
    $assortiment = array("value1","value2","value3","value4","value5","value6");
    // Here I just want to display my array using a while loop
    echo "Arrays unsorted
    ";
    echo "Assortiment: ";
    
    for($i=0;$i<sizeof($assortiment);$i++) {
    	echo $assortiment[$i] . " ";
    }
    
    
    
    
    //$i=0;
    //while($assortiment[$i]){
    //  echo $assortiment[$i] . " ";
    // $i++;
    //}
    ?>
    </body>
    </html>
    hope that helps

    v_Ln

Posting Permissions

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