php array_key_exists, !empty and isset fails for no reason?

百般思念 提交于 2020-07-23 17:10:17

问题


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:


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.



来源:https://stackoverflow.com/questions/39634340/php-array-key-exists-empty-and-isset-fails-for-no-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!