PHP Warning: Undefined Variable, Undefined Index or Undefined Array Key
These warnings mean the code read a value that was not set. Fix by checking before use.
Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Meaning
PHP is telling you the variable or array item does not exist. Newer PHP versions show these more clearly.
Fix
- Find the file and line. See seeing PHP errors.
- Make sure the variable is assigned before use, in all code paths.
- For arrays, check with
isset()or use the null coalescing operator ($_POST['name'] ?? ''). - For form fields, remember unchecked checkboxes are not sent.
- Keep display_errors off on live sites and log errors instead.
After upgrading PHP
Old code may now show more warnings. See Site Broke After a PHP Version Upgrade: Deprecated and Removed Functions.
Related guides
- YouTube Video Embed Not Working or Showing an Error on Your Website
- Yoast SEO or Rank Math Sitemap Shows 404 or Is Empty
- WordPress XML Sitemap Gives 404 or Is Not Generating
- Changed the WordPress Address (URL) and Now the Site or Login Is Broken
Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.