Example analysis of isset and array_key_exists in PHP _php tutorial

Source: Internet
Author: User
Tags php error

A case study of the difference between isset and array_key_exists in PHP


This article explains the difference between isset and array_key_exists in PHP. Share to everyone for your reference. The specific analysis is as follows:

1. For the value of the array is different, for a value of NULL or ' or false,isset return false,array_key_exists true;

2. Execution efficiency is different, isset is built-in operator, Array_key_exists is PHP built-in function, isset to be faster. Please refer to: PHP function implementation principle and performance analysis

3. When using Isset to access a non-existent index array value, it will not cause a e_notice PHP error message;

4.array_key_exists will call Get_defined_vars to determine if the array variable exists, isset not;

Test code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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[' AA ']= ' DD ';

$test _arr[' BB ']= ';

$test _arr[' cc ']=null;

$test _arr[' DD ']=false;

$test _arr= Array (' AA ' = ' dd ', ' bb ' = ', ' cc ' =>null, ' DD ' =>false);

echo "Isset aa is"; Var_dump (Isset ($test _arr[' AA ')); 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 (' AA ', $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 <100; $i + +) {

Isset ($test _arr[' AA ');

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "Isset is $time \ n";

for ($i =0; $i <10000; $i + +) {

Isset ($test _arr[' AA ');

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "Isset 10000 is $time \ n";

for ($i =0; $i <1000000; $i + +) {

Isset ($test _arr[' AA ');

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "Isset 1000000 is $time \ n";

//++++++++++++++++++++++++++++++

$time _start = Microtime_float ();

for ($i =0; $i <100; $i + +) {

Array_key_exists (' AA ', $test _arr);

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "Array_key_exists is $time \ n";

for ($i =0; $i <10000; $i + +) {

Array_key_exists (' AA ', $test _arr);

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "array_key_exists 10000 is $time \ n";

for ($i =0; $i <1000000; $i + +) {

Array_key_exists (' AA ', $test _arr);

}

$time _end = Microtime_float ();

$time = $time _end-$time _start;

echo "Array_key_exists 1000000 is $time \ n";

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1009819.html www.bkjia.com true http://www.bkjia.com/PHPjc/1009819.html techarticle The example of isset and array_key_exists in PHP explains the difference between isset and array_key_exists in PHP. Share to everyone for your reference. The specific analysis is as follows: 1. For arrays ...

  • Related Article

    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.