php array_key_exists() 與 isset() 的區別

 一個基本的區別是isset()可用於數組和變量,而array_key_exits()只能用於數組。php

可是最主要的區別在於在設定的條件下的返回值。數組

如今咱們來驗證一下這個最主要的區別。ide

array_key_exists()函數

array_key_exists() 會檢查鍵值的存在. 這個函數會返回TRUE,只要鍵值存在,即便值爲NULL.post

$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); // trueui

 isset()this

和arrry_key_exitst()不一樣,isset()會同時檢查鍵和值,只有當健存在,對應的變量不爲NUll的時候纔會返回TURE。lua

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

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.code

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.

相關文章
相關標籤/搜索