There is a PHP error message so common that the web-o-sphere is littered with literally thousands of posts asking what it means. Dozens of WordPress plugins are affected as well as untold numbers of other applications written in PHP. I encountered this error in PHP apps so often that I have avoided PHP with almost a religious fervor. Today I think I understand the problem.
The PHP user base is constantly asking what these error/alert/notices mean. By and large these questions go unanswered. That indicates an unhealthy state of affairs for the end user. I have read many posts in the PHP community that say turning off the error display is an acceptable way to deal with these errors. I disagree.
Suppression of known faults is not a wise development practice. It leads to obscure problems that are hard to diagnose. It is worthwhile to add a little bit of code that does away with the notices while in debug or system configuration mode. Proper operation with error display enabled confirms that the application code is correctly dealing with environment state.
Here is a simple way to handle the condition in a deterministic manner. For the non-technical reader, please accept my apologies for the geek speak. For the technoids, I have tested this here with success. Your mileage may vary so check with your mechanic if something is less than perfect when you try it.
The Setup
WordPress 3.4.1 running on Apache 2 and Centos 5.
Any and all other plugins are deactivated
By the way, deactivating everything except the new plugin, or whatever, is highly recommended for installing any third party components in anything anywhere.
The Problem
Install something new via WordPress “Add new plugin” install feature. When you activate it WordPress reports “The plugin generated XXX characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
”
Now there is a truly beneficent message. I wish the US Congress came with a warning like that. In WordPress, this means the program code did not load properly.
If the web site has the PHP error display feature enabled you are treated to the ever popular “undefined index” error in the top part of the web page:
Notice: Undefined index: GoThinGee in .../wp-content/plugins/wp-asaet/wp_amazingly_simple_and_easy_thingee.php on line XXX
Take a peek at the line mentioned in the error message; you are likely to find a snippet of code that looks something like this: if($_POST['GoThinGee'])
. “$_POST” is an array inside an IF statement. In most cases, the array will have stuff in it, including the value named “GoThinGee”. In the case we are looking at today the array is utterly empty and the error is that the index “GoThinGee” is not part of the array.
It only happens when the application is loaded. This can be considered a “threshold” or “initial state” problem.
The Solution
Adding a bit of defensive code to create a known state in the app environment is all that is needed to avoid the “undefined index” condition. This accomplished by assuring that the array $_POST has a set of index names with reasonable values.
In most applications, it is best to use a human readable value such as the string literal “undefined” to initialize the array. However, the application we used for this article has a large body of existing code that assumes a valid array of empty strings. Using a human readable term such as “undefined” would require changing a great deal of the existing code. For this reason, the initialization value is a pair of single quotes.
This extra code must be added prior to any evaluation of the array $_POST.
// August 20, 2012, mophilly; set default values to stop undefined index errors.
$default_string = '';
if(!isset($_POST['GoThinGee']))
{
$_POST['GoThinGee'] = $default_string;
}
What this bit of PHP code does is add an index named “GoThinGee” if it does not exist. The new index is initialized with the value of $default_string.
I hope this helps someone.
Follow