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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM