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:
<?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 $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 will help you with your PHP programming.