This article illustrates 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 judgment of the array value, return true for the value of NULL or ' or false,isset return false,array_key_exists;
2. The execution efficiency is different, isset is constructs the operator, array_key_exists is the PHP built-in function, isset must be quicker. Please refer to: PHP function implementation principle and performance analysis
3. When using Isset to access a nonexistent indexed array value, a e_notice PHP error message is not generated;
4.array_key_exists will call Get_defined_vars to determine whether 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 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60-61 |
<?php 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 $timen"; For ($i =0 $i <10000; $i + +) {isset ($test _arr[' AA ');} $time _end = Microtime_float (); $time = $time _end-$time _start; echo "Isset 10000 is $timen"; For ($i =0 $i <1000000; $i + +) {isset ($test _arr[' AA ');} $time _end = Microtime_float (); $time = $time _end-$time _start; echo "Isset 1000000 is $timen"; ++++++++++++++++++++++++++++++ $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 $timen"; 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 $timen"; 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 $timen"; |
I hope this article will help you with your PHP programming.