0

I was looking at my httpd error log and notice this error over and over again

Undefined offset: 1 in /usr/home/*****/*****/*****/*****/index.php on line 43.

I was hoping someone could look over my code and see they see any problem which would be causing this error

<?php $files = scandir('movies/');
            foreach($files as $file) {
              if ($file === '.' or $file === '..') continue;
              $t = str_replace("-", " ", $file);
              $section = explode(';', $t);
              $section = explode('.', $section[1]);
              $t = explode(';', $t);
              $t = $t[0]; 

The line that is getting the error is

$section = explode('.', $section[1]);

The code is working on the site but giving an error

yergo
  • 4,761
  • 2
  • 19
  • 41
Jay Dogg
  • 13
  • 1
  • 4
    So, check if you have `$section[1]` before trying to explode it? Pretty straight forward. – N.B. Apr 23 '15 at 15:36
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – ʰᵈˑ Apr 23 '15 at 15:45
  • Thanks for the quick reply and you are correct there are to files that had the wrong naming scheme. I just put a simple if statement to check if `$section[1]` was empty or not. I took care of the name problems as well. Thanks for all the help guys – Jay Dogg Apr 23 '15 at 16:05
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Apr 24 '15 at 03:48

1 Answers1

0

It happens, because after explode you have array only with [0] key probably, that means you have no ; in your $t in previous line and whole $t goes into first $section assignment.

Never assume you will always have exactly the data you have during code developing. You're setting traps for yourselves. I would suggest solution close to following:

$section = explode(';', $t);
$section = explode('.', count($section) > 1 ? $section[1] : $section[0]);

or

$section = explode(';', $t);
if(count($section) === 0) {
    // debug code, warnings, reports, Exceptions etc.
} else {

}
yergo
  • 4,761
  • 2
  • 19
  • 41