Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Warning: Undefined variable and Undefined array key in PHP

ErrorWarning: Undefined variable $total in /var/www/app/cart.php on line 12

Short answer

PHP read a variable or array key that was never set. In PHP 8 this is a Warning, and the old Undefined index and Undefined offset messages became Undefined array key. The value read is null and the script continues. Set the variable on every path, or read the key with isset() or the ?? operator.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the message: it names the variable or key and the file and line. Open that line and see where the value should have been set.

    php -l form.php
    grep -n "email" form.php
  2. 2Match what you see, then jump to its fix

    1. 1Warning: Undefined variable $nameA variable is set only on some code pathsFix
    2. 2Warning: Undefined array key "email" (the key can be a string or a number)An array key is read that the array does not haveFix
    3. 3The code worked on PHP 7.4, and the same line now prints Warning instead of NoticeUpgrading to PHP 8 turned a hidden notice into a visible warningFix

What does the error look like?

  • Warning: Undefined variable $total in /var/www/app/cart.php on line 12
  • Warning: Undefined array key "email" in /var/www/app/form.php on line 8
  • Warning: Undefined array key 3
  • Notice: Undefined index: email (PHP 7 and older)

Cause 1 of 3

A variable is set only on some code paths

1Check

Look for a variable assigned inside an if, a loop or a try, and then read after it. If the branch did not run, the variable does not exist.

grep -n -B3 -A3 "\$total" cart.php

2Fix

Give the variable a default before the branch. Appending with [] to an undefined variable is the one case that does not warn, because PHP creates the array.

<?php
$total = 0;
foreach ($items as $item) {
    $total += $item['price'];
}
echo $total;

3Verify

Run the page through a path that skips the branch and see no warning.

php cart.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

An array key is read that the array does not have

1Check

Typical sources are $_POST, $_GET and rows from a query. A form field that was not submitted, or a column that was not selected, is not in the array.

php -r "var_dump(\$_POST[\"email\"] ?? null);"

2Fix

Use the null coalescing operator for a default, or isset() when you need to branch. Do not use @ to hide it.

<?php
$email = $_POST['email'] ?? '';
if (isset($_POST['agree'])) {
    // checkbox was ticked
}

3Verify

Submit the form without the field and confirm no warning appears.

php -l form.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

Upgrading to PHP 8 turned a hidden notice into a visible warning

1Check

Before PHP 8.0 these were notices and error_reporting often hid them. In PHP 8 they are warnings and the message text changed: Undefined index and Undefined offset became Undefined array key.

php -v
php -i | grep -E "^error_reporting "

2Fix

Fix each place with the two patterns above. Raising the display level is not a fix: on production keep display_errors off and log the warnings, then clean the log up.

; php.ini (production)
display_errors = Off
log_errors = On

3Verify

Run the test suite or key pages and check the PHP log is free of Undefined array key lines.

grep -c "Undefined array key" /var/log/php/error.log

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Initialise variables at the top of the function or file, before any if or loop.
  • Read request input only through ?? or isset(), with a safe default.
  • Run the test suite with error_reporting = E_ALL so warnings fail early.
  • Use a static analyser such as PHPStan to find undefined variables before deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.