Notice: Undefined offset: 1 in line 60

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moran1409
Forum Newbie
Posts: 13
Joined: Wed Feb 23, 2011 12:45 pm

Notice: Undefined offset: 1 in line 60

Post by moran1409 »

i get the error for this code:

Code: Select all

	   <?php
		$news = fopen("test.txt", "rb");

		while (!feof($news) ) {

			$line = fgets($news);
			$parts = explode('=', $line);

		print $parts[0] . $parts[1]. "<BR>";//line 60!!!
		
	}

	fclose($news);

	   ?>
i know that this means that the array gets one argument instead of two but i do'nt know how to solve it...
this code works fine in my localhost but not on the server
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Notice: Undefined offset: 1 in line 60

Post by McInfo »

What do you want your program to do with lines that do not contain "="?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Notice: Undefined offset: 1 in line 60

Post by pickle »

Basically it looks like you're just taking the '=' out right? Why not just do a str_replace()?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
moran1409
Forum Newbie
Posts: 13
Joined: Wed Feb 23, 2011 12:45 pm

Re: Notice: Undefined offset: 1 in line 60

Post by moran1409 »

thanks for the help, i solved it and this is the solution:

Code: Select all

<?php

  $news = fopen("test.txt", "rb");

  while (!feof($news) ) {

    $line = fgets($news);
    $parts = explode('=', $line);

    if(count($parts) == 2) {
      echo $parts[0] , $parts[1], '<br>';
    } else {
      echo $parts[0], '<br>';
    }
               
  }

  fclose($news);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Notice: Undefined offset: 1 in line 60

Post by pickle »

You could replace

Code: Select all

if(count($parts) == 2) {
      echo $parts[0] , $parts[1], '<br>';
    } else {
      echo $parts[0], '<br>';
    }
with

Code: Select all

echo implode('',$parts).'<br />';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply