PHPisset and array_key_exists differences _ PHP Tutorial

Source: Internet
Author: User
PHPisset and array_key_exists are different. When determining whether the index value of a PHP array exists, the isset and array_key_exists methods are generally used, but the values returned by the two methods are different, the following describes whether isset and array are used to determine whether the index value of a PHP array exists. the isset and array_key_exists methods are generally used, but they return different values, next I will introduce the differences between isset and array_key_exists. if you need them, refer to them.

Let's take a look at a simple example.

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


Example 1

The code is as follows:

$ A = array ('key1' => '20140901', 'key2' => null );

Use these two methods to determine the existence of the key value:

The code is as follows:

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:

$ 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

?>

If the index value of the sort PHP array exists, the isset and array_key_exists methods are generally used, but the returned values of the two methods are different. next I will introduce isset and array...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.