Re: Undefined offset notice with explode



Richard Lawrence wrote:
Hi there,

I'm having a problem with PHP which I'm not sure how to best solve.
Here is the code:

$fp = fopen("comments.txt", "r");
while(!feof($fp))
{
  $line = fgets($fp, 1024);
  list($key, $value) = explode(":", $line, 2);

  if ($key == "something")
  {
    ...etc...etc...

The problem here is that if the line doesn't contain a : then there
isn't anything to populate $key and $value and so PHP reports "PHP
Notice:  Undefined offset:  1". When I used to work with Perl, it
wouldn't report and rely on me handling the issue later if needs be.

What is the best way to get the explode line to not report a PHP notice
when it comes across a $line that isn't in the correct format?

Many thanks,

Richard.


if (strpos($line, ':') !== false) list($key, $value) = explode(":", $line, 2); else whatever...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Stuck & Need Help with "Generate Report"
    ... I need to complete this "Generate Report" page for my employer and I'm ... What I want to do is create a single file to display and process the ... can I have this page split PHP | HTML |PHP when using the "if ... php file so that if sumbitted, here is the query and output BUT if not ...
    (comp.lang.php)
  • Re: group by report
    ... okay a little background, I know php and mysql, so most of the stuff i ... do is moving client to server to client. ... in order to make the report more readable, we sort based on the req ... the group of each unique order number. ...
    (microsoft.public.excel.misc)
  • Re: Undefined offset notice with explode
    ... Jerry Stuckle wrote: ... > Richard Lawrence wrote: ... >> I'm having a problem with PHP which I'm not sure how to best solve. ... >> wouldn't report and rely on me handling the issue later if needs be. ...
    (comp.lang.php)
  • Re: PHP has encountered an access violation...
    ... Some kind of internal bug in PHP. ... Maybe because if no one ever took the time to find problems and report ... running in 'debug' mode. ...
    (comp.lang.php)
  • Re: [PHP] Hide Warnings
    ... still writing warnings to screen. ... My ISP is using PHP Version 4.3.10 if that is any help. ... // Reporting E_NOTICE can be good too (to report uninitialized ...
    (php.general)