Which of the following is more efficient for the array functions isset and array_key_exists in PHP?

Source: Internet
Author: User

Tested:

If (isset ($ array [$ I]) {echo "exists ";}
If (array_key_exists ($ I, $ array) {echo "exists ";}

When the number of arrays is 10, the difference between the two is shown that the isset speed is nearly 10 times faster, but it is not very obvious, and there is not much difference between the two for the existence of variables, it runs faster when a variable exists, but this trend is very weak.

When array_key_exists is used, as the number of cycles increases, the program running time increases by a geometric level. When the number of elements in an array exceeds 1000, the operation speed is very slow.

Therefore, isset and array_key_exists determine whether an element in an array function exists. The isset speed is faster, and the speed difference is very large.

Because isset belongs to the language structure in php, and array_key_exists is a function, isset is faster. In addition, isset also exists in other languages and is more readable.

In addition, when the variable value is NULL, isset returns false, while array_key_exists only determines whether the variable exists. Therefore, if you determine an element in an array and whether it is true, you should use isset.


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.