Open In App

Difference between isset() and array_key_exists() Function in PHP

Last Updated : 09 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

isset() function

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

Syntax:

bool isset( $var, mixed )

Parameters: This function accepts more than one parameters. The first parameter of this function is $var. This parameter is used to store the value of variable.

Program:




<?php 
  
// Declare an array
$array = array();
  
// Use isset function
echo isset($array['geeks']) ? 'array is set.'
    'array is not set.';
?>


Output:

array is not set.

array_key_exists() Function

This is also a predefined function in PHP which checks whether an index or a particular key exists in an array or not. It does not evaluate the value of the key for any null values. It returns false if it does not find the key in the array and true in all other possible cases.

Syntax:

bool array_key_exists( $key, $array )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $key: This parameter is used to store the value to be check.
  • $array: This parameter is used to store an array with keys to check.

Program:




<?php 
  
// Create an array
$array = array(
     'name' => null,
);
  
// Use array_key_exists function
echo array_key_exists('name', $array
? 'array key exists' : 'array key does not exist';
?>


Output:

array key exists

Difference between isset() and array_key_exists() Function: The main difference between isset() and array_key_exists() function is that the array_key_exists() function will definitely tells if a key exists in an array, whereas isset() will only return true if the key/variable exists and is not null. Also isset() doesn’t render error when array/variable does not exist, while array_key_exists does.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads