When determining whether the index value of a PHP array exists, isset and array_key_exists are usually used, but the returned values are different. next I will introduce the differences between isset and array_key_exists, for more information, see.
When determining whether the index value of a PHP array exists, isset and array_key_exists are usually used, but the returned values are different. next I will introduce the differences between isset and array_key_exists, for more information, see.
The instance code is as follows:
- 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.
The instance code is as follows:
- $ A = array ('key1' => '20140901', 'key2' => null );
Use these two methods to determine the existence of the key value:
The instance code is as follows:
- Isset ($ a ['key1']); // true
- Array_key_exists ('key1', $ a); // true
- Isset ($ a ['key2']); // false
- Array_key_exists ('key2', $ a); // true
The instance code is as follows:
-
- $ 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
- ?>