Notice: Undefined index
PHP Programming Language
Severity: MinorWhat Does This Error Mean?
This notice appears when your PHP code tries to access an array element using a key that does not exist. PHP returns null for the missing key and shows this notice. It is extremely common when reading form data ($_POST, $_GET) or session values. In PHP 8, this was upgraded to 'Warning: Undefined array key'.
Affected Models
- PHP 5.x
- PHP 7.x
- Older PHP codebases
Common Causes
- Reading $_POST['fieldname'] when that field was not included in the submitted form
- Reading a session variable ($_SESSION['key']) before it has been set
- A typo in the array key — even a difference in uppercase/lowercase triggers this
- An array built conditionally that sometimes lacks certain keys
- Reading a cookie ($_COOKIE['name']) that does not exist in the browser
How to Fix It
-
Wrap the access in isset(): if (isset($_POST['email'])) { $email = $_POST['email']; }
isset() returns false if the key does not exist, preventing the notice entirely.
-
Use the null coalescing operator (PHP 7+): $email = $_POST['email'] ?? '';
This sets $email to an empty string if 'email' is not in $_POST. Short and clean.
-
For session variables, check that the session is started and the key is set before reading it.
Always call session_start() at the top of any page that uses $_SESSION.
-
Check the spelling of the array key exactly. Keys are case-sensitive: 'Email' and 'email' are different.
Also check for hidden spaces. A key of 'email ' (with a trailing space) will not match 'email'.
-
Use print_r($array) or var_dump($array) to print the full array and see exactly which keys exist.
This is a fast way to confirm what keys are available versus what you expected.
When to Call a Professional
Undefined index notices do not require professional help. They are very common in PHP development and straightforward to fix. If you are modernizing an old PHP 5 codebase that is full of these, a PHP developer can help you update it efficiently.
Frequently Asked Questions
Is Undefined index a fatal error?
No — it is just a notice. Your script keeps running. But the value you get back is null, which can cause wrong output or broken logic. Always fix notices even if the script keeps running.
How do I turn off notices in PHP?
You can change error_reporting in php.ini to exclude notices. Or add this to the top of your script: error_reporting(E_ALL & ~E_NOTICE); But it is better to fix the code than hide the notices.
Why does PHP still show notices even when the page looks fine?
PHP outputs notices to the error log or the browser alongside the page. The page can look fine while the logic behind it is subtly broken. A null value in a price calculation or username display causes quiet bugs that are hard to trace later.