-
December 29th, 2002, 03:33 PM
#1
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).
-
December 29th, 2002, 03:44 PM
#2
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
-
Forum Rules
|
|