Undefined offset on Windows not on Apache?

From: Chris Williams (chris_at_escaperadius.com)
Date: 11/30/03


To: php-general@lists.php.net
Date: Sun, 30 Nov 2003 12:31:29 -0800

I've created a script to parse through a data file which contains coma
separated fields of data. The script works on Apache, however when I run it
on under Windows (running PHP as a CGI) the script produces an Undefined
offset notice. What is strange about this notice is that the line of code
which contains the array element never gets executed.

Here is what I have:
A data file containing:
=== START OF FILE =====

1,01545,"01612,01545,01505,01609,01606,01562"

=== END OF FILE =====
The first and last lines contain carriage returns. The second or middle line
contains sample data.

The script that reads and parses the data is:

  $countLines = fopen("data.txt", "r");
  while (!feof($countLines))
  {
   $newLine = fgets($countLines, 1024);
   echo "line length: " . strlen($newLine) . "<br>";
   if(strlen($newLine) > 0)
   {
   $newLine = ereg_replace ("\"", "", $newLine);
   $zip_forward = explode(",",$newLine);
   $location_id = trim($zip_forward[0]);
   echo "array size: " . count($zip_forward) . " ID: $location_id<br>";
   $zip_to = trim(substr($zip_forward[1], 0, 5));
   }
  }
  fclose($countLines);

What's odd here is that the last line, which has a length of 0, should never
cause the if(strlen($newLine) > 0) to execute the lines of script contained
within the if conditional. And in fact the lines DON'T get executed.
However, on the Windows server I get the following error
PHP Notice: Undefined offset: 1 in \debug.php on line 20. This points to my
code that evaluates the second element of the array $zip_to =
trim(substr($zip_forward[1], 0, 5));

If I never evaluate the $zip_forward[1] array element within the if
conditional, why would PHP test the array $zip_forward for the proper number
of elements?

This script works fine under Apache.

I haven't a clue what is going on.

Comments welcome,
Chris



Relevant Pages

  • Undefined offset on Windows not on Apache?
    ... I've created a script to parse through a data file which contains coma ... which contains the array element never gets executed. ... This script works fine under Apache. ...
    (php.general)
  • Re: Arrays in bash
    ... I have this little script that sets up two lines in BASIC (or whatever ... data made by a couple number/text), and put the data in an array element ... # set first line, intentionally the second ... # set second line, which is the first ...
    (comp.unix.shell)
  • How can this be done?
    ... I am trying to manipulate the data of my @rray based on the first character ... of each array element. ... I want my script to dected the first letter of the name and take action ... I realize the script above will only do "A" ... ...
    (comp.lang.perl.misc)
  • Re: what is the meaning
    ... Also sprach Jerry Stuckle: ... USE an array element before it's been defined. ... I have heard about a security issue concerning $GLOBALS: ... passing a request parameter 'GLOBALS' to the script. ...
    (comp.lang.php)
  • Update: tomcat startup
    ... we run Apache on this system, it is not for the benefit of tomcat. ... The relevant extract of the init script now looks like this: ... I get the expected tomcat startup lines in /tmp/tomcat_start.log. ...
    (Tru64-UNIX-Managers)