dongzhenjian5195 2016-09-22 08:47
浏览 65

php array_key_exists,!empty和isset无故失败?

php 7.0.8 on ubuntu 16.0.4 LTS could somebody give me a hint why an array key check would fail when the key clearly exists, program execution just stops, with no explanation even with all error reporting enabled.

The data type held in the array element is a string containing a torrent file downloaded from fedora website https://torrents.fedoraproject.org/

other functions fail here aswell such as !empty on the array key

the key in this instance is 0

if(array_key_exists($index, $this->_webpage)){
            return $this->_webpage[$index];
        }else{
            /* throw notice */
            trigger_error("Array index is out of range. Can not get webpage.", E_USER_NOTICE);
            return FALSE;
        }

NOTE: iv been using this same function to check that webpages have been downloaded and it worked on the same system, im just adding torrent parsing to my application

  • 写回答

1条回答 默认 最新

  • dopcpc9207 2016-09-22 08:52
    关注

    SUMMARY array_key_exists will definitely tell you if a key exists in an array, whereas isset will only return true if the key/variable exists and is not null and empty return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable

    More details

    isset()

    From PHP manual – isset():

    isset — Determine if a variable is set and is not NULL

    In other words, it returns true only when the variable is not null.

    empty()

    From PHP Manual – empty():

    empty — Determine whether a variable is empty

    In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

    Also empty() does not generate a warning if the variable does not exist.

    array_key_exists

    From PHP Manual – array_key_exists():

    Checks if the given key or index exists in the array

    So array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index.

    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”