Forum Moderators: coopster

Message Too Old, No Replies

undefined offset

         

kumarsena

5:13 pm on Oct 31, 2004 (gmt 0)

10+ Year Member



wehen working with array i get thsi

undefined offset...kinda figured out why, but just wondering exactly why it occurs and how hte logic behind it works.

k

kumarsena

6:31 pm on Oct 31, 2004 (gmt 0)

10+ Year Member



this is the code that generates it,,i thought i understood why, but now i am just as wise,,,,

//if comment file exists, read and print comments
if (file_exists($comment_file))
{
$file_array = file($comment_file);

foreach($file_array as $line)
{
$line_array = explode("¦", $line);
echo $line_array[0].'<br/ >';
echo $line_array[1].'<br/ >';
echo $line_array[2].'<br/ >';
}
}

any ideas?

dreamcatcher

7:15 pm on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This previous post may help you:

[webmasterworld.com...]

ergophobe

7:47 pm on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It means that the array is not defined for that value.

if you have a variable like

$var = array('a', 'b');

$var[0] is defined and is 'a'

$var[3] is undefined and throws a warning.

Generally, all variables should be defined before you use them for the most predictable results. You can turn error reporting down and depend on the default behavior of PHP which is relatively safe, but definitely not the preferred option.

I recently checked out an open source calendar program. It had undefined variables, offsets and index errors all over because the author had developed with error reporting at the default level. When I edited the code, I found that he had several bugs that did not show up because he was depending on the default behavior. Merely correcting for this got rid of at least six bona fide bugs in his script.

Tom