Unexpected Keys In Array

<?php

$array = [null => 42, false => 43];

var_dump(array_key_exists(null, $array));  // true
var_dump(array_key_exists('', $array));    // true
var_dump(array_key_exists(false, $array)); // true
var_dump(array_key_exists(0, $array));     // true
var_dump(array_key_exists(0.0, $array));   // true

print count($array). " elements in the array
";

It is possible to put 2 elements in a PHP array, find different 5 keys with array_key_exists() or isset() and yet, still count 2 distinct elements (key wise).

The type-juggling for array keys is applied in every PHP features, to keep things easy to use.

This code is one rare way to show how it still leaks. Depending on the context, it might be very confusing.

See Also

PHP Features

Last updated: 14 July 2026