【问题标题】:Undefined index in PHP errors [duplicate]PHP错误中的未定义索引[重复]
【发布时间】:2012-11-14 09:07:10
【问题描述】:

可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Undefined index in PHP

注意:未定义索引:第 107 行 C:\xampp\htdocs\pmsx\lib\config.php 中的 NAME

这是确切的错误。这是我在文件上的代码。谢谢你的帮助。

这是我的第 107 行:

//echo "<br>" . $cdata;
// create the proper tree node if NAME attribute is set
if ($this->attr["NAME"] != "")
    $this->tags[count($this->tags) - 1] = $this->attr["NAME"];

Pastie link

【问题讨论】:

  • 这与 sql 到底有什么关系?听起来更像是一个 DOM 操作。你检查过你所在的节点是否有NAME 属性?
  • 你能给出var_dump($this-&gt;attr);的输出吗?
  • fwiw,你的第二行可以简化$this-&gt;tags[] = $this-&gt;attr["name"];
  • @ScottEvernden,据我所知,原行覆盖了最后一个元素,而你的则添加了。不过,看起来原始行只是有一个错误。

标签: php


【解决方案1】:

首先使用isset(),检查该属性是否存在。

if ( isset( $this->attr["NAME"] ) && ($this->attr["NAME"] != "") ) {
    $this->tags[count($this->tags) - 1] = $this->attr["NAME"];
}

您的错误说,NAME 属性在数组 attr 中根本不存在!

【讨论】:

    【解决方案2】:

    NAME 不是已定义的索引。你可能想要:

    if( isset($this->attr['NAME']) && $this->attr['NAME'] != "" ) {
        // ...
    

    【讨论】:

    • 有趣。该帖子由ComFreek编辑。你有关系吗? ;-)
    【解决方案3】:

    您可能想要的是!empty($this-&gt;attr['NAME']) 而不是$this-&gt;attr["NAME"]!=""。否则,当 NAME 索引是……好吧……未定义时,它会出错。

    【讨论】:

      猜你喜欢
      • 2018-04-14
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多