确定PHP中是否存在数组键的更快,更好的方法是什么? [英] What's quicker and better to determine if an array key exists in PHP?

查看:52
本文介绍了确定PHP中是否存在数组键的更快,更好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这两个示例...

Consider these 2 examples...

$key = 'jim';

// example 1
if (isset($array[$key])) {
    // ...
}

// example 2    
if (array_key_exists($key, $array)) {
    // ...
}

我有兴趣知道这两种方法是否更好.我一直使用第一个示例,但是看到很多人在此站点上使用第二个示例.

I'm interested in knowing if either of these are better. I've always used the first, but have seen a lot of people use the second example on this site.

那么,哪个更好?快点?意图更清晰?

So, which is better? Faster? Clearer intent?

推荐答案

isset()更快,但与array_key_exists()不同.

array_key_exists()纯粹检查密钥是否存在,即使该值是NULL.

array_key_exists() purely checks if the key exists, even if the value is NULL.

在哪里 如果键存在并且值是NULL,则isset()将返回false.

Whereas isset() will return false if the key exist and value is NULL.

这篇关于确定PHP中是否存在数组键的更快,更好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆