Closed Thread Icon

Topic awaiting preservation: PHP: undefined offset reading file (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30315" title="Pages that link to Topic awaiting preservation: PHP: undefined offset reading file (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP: undefined offset reading file <span class="small">(Page 1 of 1)</span>\

 
ninmonkeys
Bipolar (III) Inmate

From: mn
Insane since: May 2004

posted posted 06-09-2008 01:20

My code is generating an undefined offset. I have a text file filled with lines that may contain comments ( prefix: "//" ), or data: in the form: [number]=[string] . Like an ini file, if the key was always a number in the range 0-31.

I'm getting a PHP notice when trying to read. My data is being correctly read. But I need to fix the error causing the Notices.

quote:
PHP output: ed offset: 1 in image.php on line 170
PHP Notice: Undefined offset: 1 in image.php on line 170
PHP Notice: Undefined offset: 1 in image.php on line 170
PHP Notice: Undefined offset: 1 in image.php on line 170
PHP Notice: Undefined offset: 1 in image.php on line 170 # repeating several times



(1) To double check: if split() returns false, are these two statements
equivalent in PHP?
if( !$result) continue;
# AND:
if( $result == False ) continue;

code:
# first code; works as intented, except it outputs ugly notices
$lines = file( $path ) or die( "image.php: could not open: $path" );
	foreach( $lines as $line_num => $cur_line ) {
		$cur_line = rtrim( $cur_line );
		# this next line causes notice:
		list($cur_day, $cur_result) = split("=", $cur_line, 2); # this is line #170



code:
# so I tried to validate it, fixing only partial errors: All but 2 errors remain
$lines = file( $path ) or die( "image.php: could not open: $path" );
	foreach( $lines as $line_num => $cur_line ) {
	    $result = split("=", $cur_line, 2);

		# now this next line causes error: "Undefined offset 1"
	    if(! $result or !$result[0]) continue; # this is line #170
		if($result == False or !$result[1]) { continue;  } # not enough elements
		$cur_day = $result[0];
		$cur_result = $result[1];



now the error is:

quote:
T: 1 in image.php on line 170
PHP Notice: Undefined offset: 1 in image.php line 170
"Undefined offset 1 in image.php";



(2) What does the error "T: 1 in <file> on line <number>" mean?
(3) How do I fix the error Undefined offset?

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-09-2008 11:27

(1) yes.
(2) means you're wreaking some weird havoc that ends up swallowing the first few characters of (Undefined offse)t 1 in image.php on line 170, and acidintially also capitalizing the T .
(3) you'll need to use !isset($resultnt) if you want to check for that.
(3b) what you really want to do most likel if (!$result or count($result) != 2) { continue; }

so long,

->Tyberius Prime

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-09-2008 23:25

I don't have anything to add...just wanted to wave to ninmonkeys Haven't seen you here in quite some time.

Later,

C:\

ninmonkeys
Bipolar (III) Inmate

From: mn
Insane since: May 2004

posted posted 06-10-2008 07:30

That fixed it.

Hi CPrompt. I haven't been here for a long time.

« BackwardsOnwards »

Show Forum Drop Down Menu