drtzb06222 2013-02-02 11:28
浏览 68
已采纳

注意:未定义的偏移量:使用if()和elseif()语句[关闭]在E:\ server \ htdocs \ file.php中为1

I am using this code:

<?php
include_once('/simple_html_dom.php');
$dom = file_get_html("http://www.gsmarena.com/samsung_i9300_galaxy_s_iii-4238.php");
foreach ($dom->find('tr') as $node) {
if (is_a($node->children(0), 'simple_html_dom_node')) {
    if ($node->children(0)->plaintext == "Protection") {
        $plain = explode(',', $node->children(1)->plaintext);
    if($plain[0] === null){echo 'Nu are';}
     elseif($plain[0]!== null) {echo $plain[0];
     if($plain[1] === null){echo 'Nu are';}
     elseif($plain[1] !== null) {echo $plain[1];} 
     }  
    }

    }
}

?>

All i want is if there exist $plain[x] or $plain[y] to shows me if not to display an custom text (in my case 'Nu are'). All is fine, code works good because $plain[1] not exist in present case, but I receive an error too.

Here is the result receved from code:

Corning Gorilla Glass 2 Notice: Undefined offset: 1 in E:\server\htdocs\preluare\1.php on line 10 Nu are

First is shows me offset 0, because exist and second offset not exist and shows me that error and after error is the correct custom text.

Correct result is Corning Gorilla Glass 2 Nu are

  • 写回答

1条回答 默认 最新

  • duanpuchen3142 2013-02-02 11:31
    关注

    Instead of

    $plain[1] !== null
    

    use

    array_key_exists(1, $plain)
    

    or

    isset($plain[1])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像