Undefined offset

Discussion in 'PHP' started by lektrikpuke, Jan 21, 2009.

  1. #1
    Making a personal calendar that displays text - long description (obtained from a db) if text exists. This works but I get a server log error saying that there is an undefined offset:

    for ($i=1; $i<=$no_of_days; $i++){
    if($lng_desc[$i]){
    echo $lng_desc[$i];
    }
    }

    It's not complaining about the offset when there is a description, only on days when there is no description. I thought that the if would negate this - if true display text, if not (do nothing as there is no else). What is wrong here?

    :D
     
    lektrikpuke, Jan 21, 2009 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Change the if statement to:
    if(array_key_exists($i, $lng_desc)){
     
    phper, Jan 21, 2009 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    In your if statement, you are assuming the index exists and simply checks whether the value stored in that index computes to the boolean value 'true'. e.g.: non-empty string, non-zero int, etc.
     
    phper, Jan 21, 2009 IP
  4. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #4
    I tried if array_key_exists (thank you by the way) and received the following error:

    Undefined variable: lng_desc
    array_key_exists()
    The second argument should be either an array or an object

    More help?
     
    lektrikpuke, Jan 22, 2009 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    <?php
    
    for ( $i = 1; $i <= $no_of_days; ++$i )
    {
        if ( isset($lng_desc[$i]) )
        {
            echo $lng_desc[$i];
        }
    }
    PHP:
     
    Danltn, Jan 22, 2009 IP
  6. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    You most probably misspelled something? If you simply replaced your old if statement with the one I suggested, the notice shouldn't occur if it didn't occur with the old code.

    You could add another check in your if statement to check if $lng_desc has been defined to clear the notice, but most probably that would just give you a logic error (nothing being printed). Check why $lng_desc would suddenly be undefined there.
     
    phper, Jan 22, 2009 IP
  7. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #7
    Thanks guys. The isset fixed it. I knew about isset (and use it), but it just didn't dawn on me to use it here.
     
    lektrikpuke, Jan 22, 2009 IP
  8. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Beware that isset() has the weird behaviour that it will still return 'false' if the variable is actually set but has the value 'NULL'. Probably not an issue in your case but I'd always use array_key_exists() to be on the safe side, unless when I really want to check that the value is not null as well.
     
    phper, Jan 22, 2009 IP
  9. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #9
    PHPer,

    Thank you for the feedback. I'll try to remember the side effect of isset(). Now that I have all the other errors fixed (there were a couple I kept to myself), I'll try your suggestion again.
     
    lektrikpuke, Jan 23, 2009 IP