We help IT Professionals succeed at work.

Undefined offset: 1 using list() and split()

taylort2
taylort2 asked
on
A few times, I have gotten "Undefined offset: 1" and I can't figure out why it is happening.  Most of the time nothing happens.

Below is the code snippet


$selByReport = @$_POST['selByReport'];
 
if ($selByReport != ""   && strtoupper(substr($selByReport,0,6)) != "SELECT")
{
     list($ReportName,$ReportRevision) = split('-',$selByReport);
}

Open in new window

Comment
Watch Question

Top Expert 2007

Commented:
You get that error if there is no - in $_POST['selByReport']

Author

Commented:
The $_POST['selByReport'] is currently blank so the code should not be executing at all.  My if-statement should be stopping the code from running but once in a great while it doesn't.
Top Expert 2007
Commented:
$_POST['selByReport'] might not be empty, it might just contain something invalid like 1221 or a ,b, ..
The attached code should give you an error if the - is missing
<?php
$selByReport = @$_POST['selByReport'];
if ($selByReport != ""   && strtoupper(substr($selByReport,0,6)) != "SELECT")
{
    if (strpos($selByReport, '-') == false) {
        echo "`$selByReport` has an invalid format";
    } else {
        list($ReportName,$ReportRevision) = split('-',$selByReport);
    }
}

Open in new window

Commented:
Could it be a HTML error (like a missed bracket) in your html-form which results in sending junk data.

What does
print_r($_POST);
display?

Author

Commented:
Arikael,

The data being submitted is correct(no junk).  It's that this error has only occurred once or twice since the page has been up.

hernst42,

I was adding the extra check when I notice your code.


Thanks guys.

Author

Commented:
I usually put extra checks in my code but lately I have been required to finish ASAP then move on to the next project.  Thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.