How to avoid undefined offset

后端 未结 8 2188
梦谈多话
梦谈多话 2020-12-13 05:24

How can you easily avoid getting this error/notice:

Notice: Undefined offset: 1 in /var/www/page.php on line 149

... in this code:

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 06:31

    You get an undefined offset when the thing you're trying to explode the string by ($value) doesn't actually have it in, I believe.

    This question is very much similar to this: undefined offset when using php explode(), where there is a much further explanation which should fully solve your issue.

    As for checking for the occurrence of '|' as to prevent the error, you can do:

    $pos = strpos($value,'|');
    
    if(!($pos === false)) {
         //$value does contain at least one |
    }
    

    Hope this helps.

提交回复
热议问题