The difference between PHP isset and array_key_exists_PHP tutorial

WBOY
Release: 2016-07-13 17:14:52
Original
923 people have browsed it

When determining whether the index value of a certain PHP array exists, isset and array_key_exists are generally used. However, the values ​​returned by the two will be different. Let me introduce the difference between isset and array_key_exists. Friends in need can refer to it. .

Let’s look at a simple example first

The code is as follows
 代码如下 复制代码

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

Copy code

isset($a['key'])

array_key_exists('key', $a)array_key_exists

Tells you exactly whether a certain key exists in the array, while isset just returns the status of whether the key value is null.

isset function is to detect whether the variable is set.

Format: bool isset ( mixed var [, mixed var [, ...]] )


Return value:

 代码如下 复制代码

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

1. If the variable does not exist, return FALSE

2. If the variable exists and its value is NULL, it also returns FALSE

3. If the variable exists and the value is not NULL, return TURE
 代码如下 复制代码

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

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

4. When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be FALSE

 代码如下 复制代码

$a = array ('test' => 1, 'hello' => NULL);

var_dump( isset ($a['test') ); // TRUE
var_dump( isset ($a['foo') ); // FALSE
var_dump( isset ($a['hello') ); // FALSE

// 'hello' 等于 NULL,所以被认为是未赋值的。
// 如果想检测 NULL 键值,可以试试下边的方法。
var_dump( array_key_exists('hello', $a) ); // TRUE

?>

Example 1
The code is as follows
Copy code

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

Use these two methods to determine the existence of key values. The results are as follows:
The code is as follows Copy code
isset($a['key1']);                                                                                   array_key_exists('key1', $a); // true isset($a['key2']); // false array_key_exists('key2', $a); // true
Example 2
The code is as follows Copy code
<🎜>$a = array ('test' => 1, 'hello' => NULL); var_dump( isset ($a['test') ); // TRUE var_dump( isset ($a['foo') ); // FALSE var_dump( isset ($a['hello') ); // FALSE // 'hello' is equal to NULL, so it is considered unassigned. // If you want to detect NULL key values, you can try the following method. var_dump( array_key_exists('hello', $a) ); // TRUE ?> http://www.bkjia.com/PHPjc/628922.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628922.htmlTechArticleWhen determining whether the index value of a PHP array exists, isset and array_key_exists are generally used, but both methods The values ​​returned will be different. Let me introduce isset and array...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!