php array_key_exists() 与 isset() 的区别
Posted 咸蛋超哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php array_key_exists() 与 isset() 的区别相关的知识,希望对你有一定的参考价值。
一个基本的区别是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.
以上是关于php array_key_exists() 与 isset() 的区别的主要内容,如果未能解决你的问题,请参考以下文章
PHP 中数组函数 isset 效率比 array_key_exists 更高
深入理解PHP之isset和array_key_exists对比
PHP array_key_exists 不起作用;数组不是多维的