PHP determines whether an array of key values exists, using empty or isset or array_key_exists

Source: Internet
Author: User

PHP determines whether an array of key values exists, using empty or isset or array_key_exists

Purpose of this article

The first few days of work, it is necessary to frequently judge the existence of the key values in the array, initially using the "!empty ($arr [' key '])" to determine that this is more comfortable to read, but the written code can not pass the Unit test (unit test is good). Troubleshooting for a long time, finally found that when $arr[' key ' = = 0 o'clock, empty still returns true, which destroys the business logic!

Today is finally free, will judge the array key value of the existence of methods to look up, recorded in this, as a memo.

An example

Guess what the following example will output?

1234567891011121314 <?php$a = array(‘a‘=>1, ‘b‘=>0, ‘c‘=>NULL);echo ‘a test by empty: ‘ , empty($a[‘a‘]) ? ‘not exist‘ : ‘exist‘, PHP_EOL;echo ‘a test by isset: ‘ , isset($a[‘a‘]) ? ‘exist‘ : ‘not exist‘, PHP_EOL;echo ‘a test by array_key_exists: ‘ , array_key_exists(‘a‘, $a) ? ‘exist‘ : ‘not exist‘, PHP_EOL, PHP_EOL;echo ‘b test by empty: ‘ , empty($a[‘b‘]) ? ‘not exist‘ : ‘exist‘, PHP_EOL;echo ‘b test by isset: ‘ , isset($a[‘b‘]) ? ‘exist‘ : ‘not exist‘, PHP_EOL;echo ‘b test by array_key_exists: ‘ , array_key_exists(‘b‘, $a) ? ‘exist‘ : ‘not exist‘, PHP_EOL, PHP_EOL;echo ‘c test by empty: ‘ , empty($a[‘c‘]) ? ‘not exist‘ : ‘exist‘, PHP_EOL;echo ‘c test by isset: ‘ , isset($a[‘c‘]) ? ‘exist‘ : ‘not exist‘, PHP_EOL;echo ‘c test by array_key_exists: ‘ , array_key_exists(‘c‘, $a) ? ‘exist‘ : ‘not exist‘, PHP_EOL, PHP_EOL;

The output results are as follows:

========================================================

A test by empty:exist

A test by isset:exist

A test by array_key_exists:exist

B Test by empty: not exist

B Test by Isset:exist

B Test by Array_key_exists:exist

C Test by empty: not exist

C Test by Isset: not exist

C Test by Array_key_exists:exist

========================================================

Note the Red highlight part

Grammatical differences in three different ways

    • Empty: When the parameter is 0 or null (as shown above), empty returns True, details can be found in the official empty manual
    • isset: When the parameter is NULL, the return false,0 is different from null in PHP, isset (0) returns True
    • array_key_exists: Purely judging whether an array key-value pair exists, regardless of the value

So, from the point of view of accuracy,array_key_exists is the most accurate!

Performance comparison in three different ways

To get a set of data from the Web, see here or resources as follows:

For a small array:

Array_key_exists:float (0.18357992172241)

Empty:float (0.072798013687134)

Isset:float (0.070242881774902)

For a relative big array:

Array_key_exists:float (0.57489585876465)

Empty:float (0.0068421363830566)

Isset:float (0.0069410800933838)

You can see that in the case of big data, the performance of empty and Isset is 2 orders of magnitude faster than array_key_exists, the difference is still very large. If you judge frequently, you still need to optimize. The reason for such a big performance difference, personal speculation, may be isset and empty as PHP syntax structure is not a function, PHP interpreter did the optimization, and array_key_exists as a function, no related optimizations. The specific reason, need to pass the source code exquisite.

Three ways to use recommendations

(Given that empty is similar to isset performance, but the isset accuracy is high, this compares only Isset and array_key_exists)

    • If the array is not likely to have a value of NULL, it is recommended to use Isset
    • If a value of NULL is often present in an array, it is recommended to use Array_key_exists
    • If the values in the array may appear to be null, but less so, it is recommended to combine isset with array_key_exists, such as "if (isset ($arr [' key ')] | | array_key_ Exists (' key ', $arr)) {/*do somthing*/}". This approach takes into account both performance and accuracy, but the code becomes longer.

PHP Determines whether an array of key values exists, using empty or isset or array_key_exists

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.