2

I get an undefined offset error only when I deal with files using the explode statement. It says the array created by the explode statement is undefined, but not when I deal with normal explode statements as per the code in the commented lines.

What is the explanation?

Extra information -- I have certainly used tab characters in the file:

 <?php
     $file = "../hai/jobs.txt";
     if (file_exists($file))
     {
         $handle = fopen($file, "r");
         if (!empty($file))
         {
             while (!feof($handle))
             {
                 $curLine = fgets($handle);
                 if ($curLine != "")
                 {
                     $line = explode("\t", $curLine);

                     echo $line[1];

                     //$a = "hai,how,are,you,going";
                     //$array = explode (",", $a);

                     //echo $array[1];
                 }
             }
         }
         fclose($handle);
     }
 ?>

 </body>
 </html>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brad Bal
  • 21
  • 5
  • 1
    If you have a line in your file which does not have a tab character in it, there will be no `[1]` element from the explosion. Please show you file content and your ecpected result. There is more than one way to skin this cat. P.s. `!empty($file)` is a senseless check. – mickmackusa Jan 03 '20 at 05:43
  • yes I used tab characters inside the file, This is how I inserted the contents into file---`$contents = $pid. " \t ".$title." \t".$description." \t ".$closing_date." \t ".$state." \t " .$contract." \t ".$position." \t ".$Application."\n";` – Brad Bal Jan 03 '20 at 06:38
  • It would be better if you added arguments here in comments to question as to why it should be reopended. As reopen close voters may not see the other comment. – Peter Mortensen May 10 '21 at 14:49

1 Answers1

-1

I believe that the problem is simply the divider: are you using \t (tabs) in your file at all? If you use the same text as in the commented example then probably you want to switch to commas (,).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bartosz Pachołek
  • 1,278
  • 1
  • 8
  • 17
  • No, this is wrong answer, I used tab characters inside the file, This how I inserted the contents inside the file---`$contents = $pid. " \t ".$title." \t".$description." \t ".$closing_date." \t ".$state." \t " .$contract." \t ".$position." \t ".$Application."\n";` – Brad Bal Jan 03 '20 at 14:40
  • My question has been closed for an unfair reason, The person who closed the question assumes that I have no "tab characters" ("\t") in my file and I have mistakenly input only "comma characters"(",") and wrongly used tab character ("\t") as a divider in explode statement, He believes that's why I get the Warning message, His assumption is wrong, my file has tab characters and it doesn’t even have a single comma character, and also the question is not associated with the question he mentions, Therefore my question has been wrongly suggested as duplicate, I need help to reopen the question – Brad Bal Jan 03 '20 at 22:15