php array_key_exists,!empty和isset无缘无故失败? [英] php array_key_exists, !empty and isset fails for no reason?
问题描述
ubuntu 16.0.4 LTS上的php 7.0.8可能会给我一个提示,为什么当键明显存在时数组键检查会失败,程序执行只是停止,即使启用了所有错误报告也没有解释。
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.
数组元素中保存的数据类型是一个字符串,其中包含从fedora网站 https://torrents.fedoraproject.org/
The data type held in the array element is a string containing a torrent file downloaded from fedora website https://torrents.fedoraproject.org/
其他功能在这里也会失败,例如数组键上的!empty
other functions fail here aswell such as !empty on the array key
此例中的键为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;
}
注意:iv使用同一功能检查网页是否已下载并它在同一系统上工作,我只是将种子解析添加到我的应用程序中
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
推荐答案
摘要 array_key_exists 肯定会告诉您数组中是否存在键,而 isset 仅在键/变量存在且不为null和 empty 时返回true。如果变量为空字符串,false,array(),NULL, 0?,0和未设置的变量,则返回true
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
更多详细信息
来自PHP手册– isset():
From PHP manual – isset():
isset —确定是否设置了变量,并不是NULL
isset — Determine if a variable is set and is not NULL
换句话说,仅当变量不为null时,它才返回true。
In other words, it returns true only when the variable is not null.
来自PHP手册– empty():
From PHP Manual – empty():
空-确定变量是否为空
换句话说,如果变量为空字符串,则返回true,否则返回false,array(),NULL, 0,0 ,以及未设置的变量。
In other words, it will return true if the variable is an empty string, false, array(), NULL, "0?, 0, and an unset variable.
此外 empty()如果变量不存在也不会生成警告。
Also empty() does not generate a warning if the variable does not exist.
从PHP手册– array_key_exists():
From PHP Manual – array_key_exists():
检查给定键或索引在数组中是否存在
Checks if the given key or index exists in the array
因此,如果在数组中设置了给定键,则array_key_exists()返回TRUE。键可以是数组索引的任何值。
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.
这篇关于php array_key_exists,!empty和isset无缘无故失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!