Let's take a look at a simple example.
| The code is as follows: |
Copy code |
Isset ($ a ['key']) Array_key_exists ('key', $ a) array_key_exists |
It tells you exactly whether a key exists in the array, while isset only returns whether the key value is null.
The isset function checks whether variables are set.
Format: bool isset (mixed var [, mixed var [,...])
Return value:
1. If the variable does not exist, FALSE is returned.
2. If the variable exists and its value is NULL, FALSE is also returned.
3. If the variable exists and the value is not NULL, true is returned.
4. When multiple variables are checked at the same time, TRUE is returned only when each individual item meets the previous requirement; otherwise, the result is FALSE.
Example 1
| The code is as follows: |
Copy code |
$ A = array ('key1' => '20140901', 'key2' => null ); |
Use these two methods to determine the existence of the key value:
| The code is as follows: |
Copy code |
Isset ($ a ['key1']); // true 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 |
<? Php $ 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 to be unassigned. // If you want to check the NULL key value, try the following method. Var_dump (array_key_exists ('hello', $ a); // TRUE ?> |