Help With... Notice: Undefined offset: 1

Discussion in 'PHP' started by garye, Nov 29, 2007.

  1. #1
    Hi,

    I'm having a notice message come up with the following code:

    LINE2: $qs = getenv("QUERY_STRING");
    LINE3: $split = explode("=", $qs);
    LINE4: $firstvar = $split[0];
    LINE5: $secondvar = $split[1];
    Code (markup):
    The problem is coming from LINE5. The error I get is:
    Notice: Undefined offset: 1

    The problem only occurs when $split[1] doesn't contain any information. I've tried to remove the notice by using an IF ... eg. if ($split[1]) { secondvar = $split[1]; } but it still spat out the same warning.

    Anyone with a bit more experience help me out?

    Many thanks!
    Gary
     
    garye, Nov 29, 2007 IP
  2. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Not that nice, but I always use it :)

    $secondvar = @$split[1];

    or, better, but more typing :)

    if (isset($split[1])) $secondvar = $split[1]; else $secondvar = null;
     
    Kwaku, Nov 29, 2007 IP
  3. garye

    garye Active Member

    Messages:
    79
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #3
    Thanks Kwaku!

    Yes, if this was a script that I am writing just for ME then I would have used the silencer (@) :)

    Thank you very much for your help... this bit of code has sorted it right out:
    if (isset($split[1])) $secondvar = $split[1]; else $secondvar = null;
    Code (markup):
    Warmly,
    Gary
     
    garye, Nov 29, 2007 IP