Differences between isset and array_key_exists in PHP

Source: Internet
Author: User
Tags php error
Analyze the differences between isset and array_key_exists in PHP. Differences between isset and array_key_exists in PHP. Share it with you for your reference. The specific analysis is as follows: 1. analyze the differences between isset and array_key_exists in the array PHP

This article describes the differences between isset and array_key_exists in PHP. Share it with you for your reference. The specific analysis is as follows:

1. the array values are different. if the values are null or ''or false, isset returns false, and array_key_exists returns true;

2. the execution efficiency is different. isset is a built-in operator, and array_key_exists is a php built-in function. isset is faster. See: PHP function implementation principles and performance analysis

3. when I use isset to access a non-indexed array value, it will not cause an E_NOTICE php error message;

4. array_key_exists will call get_defined_vars to determine whether the array variable exists, and isset does not need;

Test code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

Function

Microtime_float ()

{

List ($ usec, $ sec) = explode ("", microtime ());

Return (float) $ usec + (float) $ sec );

}

$ Test_arr ['A'] = 'DD ';

$ Test_arr ['BB '] = '';

$ Test_arr ['CC'] = NULL;

$ Test_arr ['DD'] = false;

$ Test_arr = array ('a' => 'DD', 'BB '=> '', 'CC' => null, 'DD' => false );

Echo "isset aa is"; var_dump (isset ($ test_arr ['A']); echo "\ n ";

Echo "isset bb is"; var_dump (isset ($ test_arr ['BB']); echo "\ n ";

Echo "isset cc is"; var_dump (isset ($ test_arr ['CC']); echo "\ n ";

Echo "isset dd is"; var_dump (isset ($ test_arr ['CC']); echo "\ n ";

Echo "isset none is"; var_dump (isset ($ test_arr ['none']); echo "\ n ";

Echo "key_exist aa is"; var_dump (array_key_exists ('A', $ test_arr); echo "\ n ";

Echo "key_exist bb is"; var_dump (array_key_exists ('BB', $ test_arr); echo "\ n ";

Echo "key_exist cc is"; var_dump (array_key_exists ('CC', $ test_arr); echo "\ n ";

Echo "key_exist dd is"; var_dump (array_key_exists ('DD', $ test_arr); echo "\ n ";

Echo "key_exist none is"; var_dump (array_key_exists ('none', $ test_arr); echo "\ n ";

$ Time_start = microtime_float ();

For ($ I = 0; I I <100; $ I ++ ){

Isset ($ test_arr ['A']);

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "isset 100 is $ time \ n ";

For ($ I = 0; I I <10000; $ I ++ ){

Isset ($ test_arr ['A']);

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "isset 10000 is $ time \ n ";

For ($ I = 0; I I <1000000; $ I ++ ){

Isset ($ test_arr ['A']);

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "isset 1000000 is $ time \ n ";

// ++

$ Time_start = microtime_float ();

For ($ I = 0; I I <100; $ I ++ ){

Array_key_exists ('A', $ test_arr );

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "array_key_exists 100 is $ time \ n ";

For ($ I = 0; I I <10000; $ I ++ ){

Array_key_exists ('A', $ test_arr );

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "array_key_exists 10000 is $ time \ n ";

For ($ I = 0; I I <1000000; $ I ++ ){

Array_key_exists ('A', $ test_arr );

}

$ Time_end = microtime_float ();

$ Time = $ time_end-$ time_start;

Echo "array_key_exists 1000000 is $ time \ n ";

I hope this article will help you with php programming.

Examples in this article describes the differences between isset and array_key_exists in PHP. Share it with you for your reference. The specific analysis is as follows: 1. for arrays...

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.