1

I have a code like this:

// load the file
self::$_list = array();
$handle = fopen($file, 'r');
while (!feof($handle)) {
    $line = fgetcsv($handle);
    if (null != $line && count($line)!=2) continue;
    self::$_list[trim($line[0])] = trim($line[1]);
}
fclose($handle);

But when I test, I got an error like this:

Notice: Trying to access array offset on value of type bool in

The error refers to the script self::$_list[trim($line[0])] = trim($line[1]);.

2
  • Sorry, but using of array() is deprecated on 7.4? Commented Jun 17, 2020 at 0:30
  • nope, its fine. no mention of deprecation php.net/manual/en/function.array.php Commented Jun 17, 2020 at 19:41

1 Answer 1

3

as you can read here in the Return section:

fgetcsv() returns NULL if an invalid handle is supplied or FALSE on other errors, including end of file.

So the problem is $line[0] when you reach the end of file, because $line is false.

Please consider having a look at the examples below on that page, and use those examples as "guide" to rewrite your function, or at lease add to the middle if something to check that condition, like:

if ( null != $line && count($line) != 2 || $line === false ) continue;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.