PHP DISCUSSION

PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

Started by salah PHP undefined array keynull coalescing operatorisset vs emptyarray_key_existsform input validation
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

salah PHP Forum
#1

After moving a form handler to PHP 8, the log is full of Warning: Undefined array key "interval" and Warning: Undefined variable $unit. The old server showed nothing. The code reads $_POST['interval'] directly, and $unit is only assigned inside an if branch.

What is the proper way to handle values that may be missing? I see isset(), empty(), ?? and array_key_exists() all recommended, and I would rather fix the cause than hide the messages.

Community replies 4

Re: PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

#2

The messages existed before; PHP 8 raised their level. Reading a missing array key was a notice ("Undefined index") in PHP 7 and is a warning in PHP 8, and the same happened for undefined variables. In both versions the expression evaluates to null and the script carries on, which is exactly why it is worth fixing: the code is computing with null where you expected a value.

For the variable, give it a value on every path: $unit = 'C'; before the if, so the branch only overrides a default.

Re: PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

#3

For optional input, the null coalescing operator is the compact answer: $interval = $_POST['interval'] ?? 60;. It returns the left side if it exists and is not null, otherwise the right side, and it raises no warning for a missing key at any depth, so $cfg['net']['port'] ?? 1883 is safe even when $cfg['net'] is absent. $x ??= 10; assigns only if $x is unset or null.

Do not confuse it with the short ternary ?:, which still warns on a missing key and also replaces every falsy value, including 0 and the empty string.

Re: PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

#4

The three tests differ on edge cases. isset($a['k']) is true only if the key exists and its value is not null. array_key_exists('k', $a) is true whenever the key exists, even with a null value, so use it when null is meaningful, for example a decoded JSON field that is present but null. empty($a['k']) is true for a missing key and also for '', 0, '0', [], false and null.

That last one causes real bugs in forms: an interval of 0 or a field containing the single character 0 is treated as not filled in. For required fields, test isset and then compare against ''.

Re: PHP 'Undefined array key' and 'Undefined variable' warnings: isset, ?? or array_key_exists?

#5

Existence is only the first check; the value is still untrusted text. $interval = filter_input(INPUT_POST, 'interval', FILTER_VALIDATE_INT); handles both in one call: it returns null if the field was not sent, false if it is not an integer, and the integer otherwise. Range limits can be passed in the options array. Remember that unchecked checkboxes are not sent at all, so their absence is normal and needs a default.

Resist suppressing the warnings with @ or a lower error_reporting. Keep E_ALL everywhere, display errors in development, and on the production server turn display_errors off and log instead.

TEP COMMUNITY