Codectionary / Developer documentation / PHP

PHP absent keys versus null

array_key_exists tests whether a key is present even when its value is null. isset answers a different question: it returns false for missing keys and keys containing null.

Syntax

array_key_exists($key, $record)

Examples

PHP absent keys versus null

Run as a PHP 8.0+ CLI script, for example php example.php. The multibyte example additionally requires mbstring.

<?php
$profile = ['nickname' => null];
var_export(array_key_exists('nickname', $profile)); echo PHP_EOL;
var_export(isset($profile['nickname'])); echo PHP_EOL;
var_export(array_key_exists('city', $profile)); echo PHP_EOL;
  1. Output: true, false, false. An explicitly empty nickname can be distinguished from an omitted field.

Best practices

  • Choose existence checks according to your schema. This function checks only the requested array level, not nested paths.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading