Home  >  Article  >  Backend Development  >  The difference between isset and array_key_exists in PHP functions_PHP tutorial

The difference between isset and array_key_exists in PHP functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:58:54784browse

When determining whether the index value of a certain PHP array exists, two methods, <font face="NSimsun">isset</font> and <font face="NSimsun">array_key_exists</font>, are generally used.

isset($a['key'])
array_key_exists('key', $a)

<font face="NSimsun">array_key_exists</font> tells you exactly whether a certain key exists in the array, while <font face="NSimsun">isset</font> just returns the status of whether the key value is <font face="NSimsun">null</font>. That is, assuming the following array is given:

$a = array('key1' => '123', 'key2' => null);

Use these two methods to determine the existence of key values. The results are as follows:

isset($a['key1']);             // true
array_key_exists('key1', $a);  // true

isset($a['key2']);             // false
array_key_exists('key2', $a);  // true

From the perspective of the PHP engine itself, the bottom layer is implemented in C language. Both <font face="NSimsun">array_key_exists</font> and <font face="NSimsun">isset</font> should be very fast. If the number of operations is thousands or tens of thousands, the performance of <font face="NSimsun">isset</font> should be more significant in this case.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363774.htmlTechArticleTo determine whether the index value of a PHP array exists, isset and array_key_exists are generally used. isset($a['key'])array_key_exists('key', $a) array_key_exists exactly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn