[Resolved] Easy fix: PHP Notice: Undefined index: clean_optimize
Hi there, very helpful plugin for debugging my use of transients for caching!
When I look at the PHP Error log for my site I see a small error that can be easily fixed. Here’s what I get in the log:
[02-Apr-2014 22:37:46] PHP Notice: Undefined index: clean_optimize in /•••/wp-content/plugins/artiss-transient-cleaner/includes/options-general.php on line 23
PHP notices aren’t a huge deal, but it’s important that they are cleared up because otherwise they create noise in error logs that developers want to watch for real errors that come up in their code. Luckily they are very easy to fix.
Here’s the section of your plugin causing the notice:
$options[ 'clean_enable' ] = $_POST[ 'clean_enable' ]; $options[ 'clean_optimize' ] = $_POST[ 'clean_optimize' ]; $options[ 'upgrade_enable' ] = $_POST[ 'upgrade_enable' ]; $options[ 'upgrade_optimize' ] = $_POST[ 'upgrade_optimize' ];
The notice happens when $_POST doesn’t contain a key called ‘clean_optimize’. It’s against the rules of PHP to use a key that doesn’t exist, even though the effect seems the same as if it existed but was empty.
What you need to do is check each value before using it to see if it’s there, like:
if (isset($_POST[ 'clean_optimize' ])) $options[ 'clean_optimize' ] = $_POST[ 'clean_optimize' ];
You should do this any time you are checking the contents of $_POST or any other array where you’re not sure if the key will exist or not.
It’s easy to find these errors while working on your plugin by keeping the WP_DEBUG constant enabled on your test installation. This will show you the errors as they come up, and is standard practice, you can read all about it on the Codex:
- The topic ‘[Resolved] Easy fix: PHP Notice: Undefined index: clean_optimize’ is closed to new replies.