php array_key_exists() 与 isset() 的区别

本文介绍了PHP中isset()与array_key_exists()函数的主要区别,并通过示例对比了两者在检查数组键值存在性方面的行为差异。此外,文章还介绍了一种新的PHP单元测试工具atoum。

 一个基本的区别是isset()可用于数组和变量,而array_key_exits()只能用于数组。

但是最主要的区别在于在设定的条件下的返回值。

现在我们来验证一下这个最主要的区别。

array_key_exists()

array_key_exists() 会检查键值的存在. 这个函数会返回TRUE,只要键值存在,即使值为NULL.

$arr = array( "one"=>"1", "two"=>"2", "three"=>null ); 
array_key_exists("one", $arr); // true 
array_key_exists("two", $arr); // true 
array_key_exists("three", $arr); // true

 isset()

和arrry_key_exitst()不同,isset()会同时检查键和值,只有当健存在,对应的变量不为NUll的时候才会返回TURE。

$arr = array( "one"=>"1", "two"=>"2", "three"=>null );
isset($arr["one"]); // true 
isset($arr["two"]); // true 
isset($arr["three"]); // false

The SitePointphp blog has a tutorial posted introducing you to a more recent addition to the testing tools available to PHP: atoum . The tutorial provides the basics and shows you how to use it in testing your code as an alternative to PHPUnit.

f you’ve been around PHP for more than a little while, you’ve no doubt started to test your code. And if you ask anyone in the PHP space what to use for writing unit tests, likely the first answer that they’ll give you is PHPUnit.

It’s the de facto standard in the PHP community, and with good reason. But it’s not the only choice. Whilst it does command the lion’s share, other choices abound, one of which I’m going to take you through in this tutorial; it’s called atoum .

They briefly introduce the tool (a "simple, modern, and intuitive unit testing framework for PHP") and help you get it installed. They also recommend installing the "atoum/stubs" package as well, making it easier to do autocomplete in most IDEs. From there the tutorial helps you configure your atoum installation to allow for code coverage reports to be generated. With things configured nicely, the next step is creating a first test evaluating a simple method that either works correctly or throws an exception. Code is included showing how to use the testing to set up expectations and evaluate the results of method execution. Finally they show the command to execute the test(s) and what the resulting code coverage reports look like.

转载于:https://www.cnblogs.com/2881064178dinfeng/p/6170955.html

### 回答1: array_key_exists 函数用于检查指定的键名是否存在于指定的数组中。它的语法如下: bool array_key_exists ( mixed $key , array $array ) 其中,$key 是要检查的键名,$array 是要检查的数组。 如果给定的 $key 在 $array 中存在,则该函数返回 true,否则返回 false。 举个例子: $arr = array('a' => 1, 'b' => 2, 'c' => 3); if (array_key_exists('a', $arr)) { echo '存在'; } else { echo '不存在'; } // 输出 "存在" 注意:该函数只能检查数字键名和字符串键名,不能检查其他类型的键名,例如对象。 ### 回答2: array_key_exists是一个用于检查数组中是否存在指定键名的函数。它的语法是array_key_exists(key, array),其中key表示要检查的键名,array表示要检查的数组。 当给定的键名存在于数组中时,函数返回true;否则返回false。这个函数在判断一个数组中是否包含某个键名的时候非常实用。 使用array_key_exists函数可以避免在检查数组键名之前,使用isset函数进行判断,因为在数组中键名存在但其值为null的情况下,isset函数会返回false,而array_key_exists函数会返回true。 下面是一个使用array_key_exists函数的例子: ```php $fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange"); if (array_key_exists("apple", $fruits)) { echo "数组中存在键名为apple的元素"; } else { echo "数组中不存在键名为apple的元素"; } ``` 在上面的例子中,我们定义了一个水果数组$fruits,并使用array_key_exists函数检查数组中是否存在键名为"apple"的元素。由于该键名存在,所以函数返回true,最终输出"数组中存在键名为apple的元素"。 总的来说,array_key_exists函数是用于检查数组中是否存在指定键名的便捷函数,可以帮助我们在处理数组时进行必要的判断和逻辑处理。 ### 回答3: array_key_exists是一个用于检查数组中是否存在指定键名的函数。它接受两个参数,第一个参数是要检查的键名,第二个参数是要检查的数组。 当数组中存在指定的键名时,array_key_exists返回true;否则,返回false。in_array不同的是,array_key_exists只检查数组的键名,而不检查键值。 这个函数的作用在于帮助我们确定数组中是否存在特定的键名,特别是当我们不确定数组中是否包含某个键名时,可以使用它来进行判断。 下面是一个使用array_key_exists的示例: ```php $fruits = array("apple" => "苹果", "banana" => "香蕉", "orange" => "橙子"); if (array_key_exists("banana", $fruits)) { echo "数组中存在键名'banana'。"; } else { echo "数组中不存在键名'banana'。"; } ``` 上述代码中,我们首先创建了一个包含苹果、香蕉和橙子的水果数组。然后,使用array_key_exists函数来检查数组中是否存在键名'banana'。由于数组中包含这个键名,因此函数返回true,并输出"数组中存在键名'banana'"。 总之,array_key_exists是一个用于检查数组中是否存在指定键名的函数,可以帮助我们确定数组中是否包含某个键名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值