简体   繁体   English

用PHP中的isset()替换array_key_exists()

[英]Replacing array_key_exists() with isset() in PHP

Knowing the differences of array_key_exists() and isset() in PHP, I see a lot of advocates on the web suggesting for replacing array_key_exists() with isset() , but I am thinking is it safe to do so? 了解PHP中array_key_exists()isset()的区别,我在网络上看到很多拥护者建议用isset()替换array_key_exists() isset() ,但是我认为这样做安全吗?

In one project, I have the value of $var submitted by users. 在一个项目中,我具有用户提交的$ var值。 The value can be anything, even NULL or not set at all. 该值可以是任何值,甚至可以为NULL或完全不设置。 I want to use !empty($var) to check if $var holds a non-empty value, but I understand that using !empty($var) alone is dangerous since if $var isn't pre-defined, PHP will throw an error. 我想使用!empty($var)来检查$var持有非空值,但我知道单独使用!empty($var)是危险的,因为如果未预先定义$var ,PHP将抛出一个错误。

So I have isset($var) && !empty($var) for checking whether $var holds a non-empty value. 所以我有isset($var) && !empty($var)来检查$var是否持有非空值。

However, things get complicated when I have a value stored in an assoc array. 但是,当我将一个值存储在assoc数组中时,事情就变得复杂了。 Compare the followings, assuming array $arr always exists but the key foo may or may not exist in $arr . 比较以下内容,假设数组$arr始终存在,但键foo可能存在或可能不存在于$arr

// code snipplet 1
$arr = array();
echo isset($arr['foo']);

// code snipplet 2
$arr = array();
echo array_key_exists('foo', $arr) && !is_null($arr['foo']);

Code snipplet 2 will always work but it seems clumsy and harder to read. 代码片段2始终可以使用,但看起来笨拙且难以阅读。 As for code snipplet 1 , I had bad experience... I wrote something like that before in the past on a development machine. 至于代码片段1 ,我的经历很糟糕……我以前在开发机器上写过类似的东西。 It ran fine, but when I deployed the code to the production machine, it threw errors simply because key didn't exist in array. 它运行得很好,但是当我将代码部署到生产机器上时,它仅仅因为数组中不存在键而引发了错误。 After some debugging, I found that the PHP configs were different between the development and the production machines and their PHP versions are slightly different. 经过一些调试后,我发现开发机器和生产机器之间的PHP配置不同,它们的PHP版本也略有不同。

So, I am thinking is it really that safe to just replace array_key_exists() with isset() ? 所以,我在想用isset()替换array_key_exists()真的安全吗? If not, what can be of better alternatives of code snipplet 2 ? 如果不是,那么代码片段2的更好替代方案是什么?

In one project, I have the value of $var submitted by users. 在一个项目中,我具有用户提交的$ var值。

How does that work? 这是如何运作的? Users shouldn't be able to set variables . 用户不应设置变量 They should be able to, eg, submit form values which end up in $_POST . 他们应该能够例如提交以$_POST结尾的表单值。 I repeat, they should not be able to directly create variables in your scope . 我再说一遍,它们应该不能直接在您的范围内创建变量

If "users" here means some sort of plugin system where people write and include PHP code… then you may want to think about defining a more stable interface than setting variables. 如果“用户”是指人们在其中编写并include PHP代码的某种插件系统,那么您可能需要考虑定义一个比设置变量更稳定的接口。

The value can be anything, even NULL… 该值可以是任何值,甚至可以为NULL…

Not if it's a value submitted through HTTP. 如果这是通过HTTP提交的值,则不会。 HTTP has no concept of null . HTTP没有null概念。 It's either an empty string or doesn't exist at all. 它要么是一个空字符串,要么根本不存在。

using !empty($var) alone is dangerous since if $var isn't pre-defined, PHP will throw an error 单独使用!empty($var)是危险的,因为如果未预先定义$var ,PHP将抛出错误

That is wrong. 那是错的。 empty specifically exists to test a variable against false without throwing an error. 专门存在empty来测试变量是否为假,而不抛出错误。 empty($var) is the same as !$var without triggering an error if the variable doesn't exist. empty($var)!$var相同,但是如果变量不存在则不会触发错误。

So I have isset($var) && !empty($var) for checking whether $var holds a non-empty value. 所以我有isset($var) && !empty($var)来检查$var是否持有非空值。

See Why check both isset() and !empty() . 请参阅为什么同时检查isset()和!empty() (Spoiler: it's redundant.) (剧透:这是多余的。)

 echo isset($arr['foo']); echo array_key_exists('foo', $arr) && !is_null($arr['foo']); 

These both do exactly the same thing. 这些都做完全相同的事情。 isset returns true if the value exists and its value is not null . 如果值存在并且其值不为nullisset返回true The second line returns true if the array key exists and its value is not null . 如果数组键存在并且其值不为null则第二行返回true Same thing. 一样。

I had bad experience... 我的经历很糟糕...

You'd need to be more detailed about that, since there should be no caveat to isset as you describe it. 你需要大约是更详细的,因为不应该有一点需要注意isset你描述它。

See The Definitive Guide To PHP's isset And empty and Difference between isset and array_key_exists . 请参阅《 PHP的isset和empty权威指南》以及isset和array_key_exists之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM