I was working on a WordPress plugin today and came across the most disturbing PHP warning error which I have never seen before in all the time that I’ve worked with PHP. The warning error message was :
Warning: Cannot use a scalar value as an array
What caused this error was when I tried to append an array key, value pair to a variable equal to zero (0). I retrieved the global WordPress variable $errors and attempted to append a key, value pair to the variable, though the variable type was invalid. The type was INT (because it was set to null) and it didn’t make sense to treat it as an array.
A solution to this would be to check if the variable is an array. In case it isn’t and it is empty, you can redefine it with the correct variable type. Usually, in PHP, this would not be the case and I cannot explain in detail exactly why this happened, but it had to have something to do with the fact that it was a pre-defined GLOBAL variable. So in my custom function, I did this :
global $errors;
if (!is_array($errors)) {
$errors = array();
}
$errors[‘field_name’] = __(‘<strong>ERROR:</strong> Please fill in a value’, $plugin_name);
}
And that fixed it! The warning error was gone and I am satisfied.







November 19th, 2008 at 1:46 pm
I also had this problem. It’s due to WordPress setting PHP’s error reporting level more strict than the default settings on your server. Most servers disable warnings since they’re not critical errors (won’t cause your script to die). WordPress overrides these settings, and it does it in at least 7 locations. Get a decent editor like jedit and do a find in directory for the php function “error_reporting”. Comment out every one.
There is absolutely no reason for WordPress to do this, other than to assume that they’re the only thing you have running under PHP on that server. It’s bad form in my book.
May 6th, 2009 at 5:40 pm
I am having a similar error in my site and I cannot figure out how to fix it. I get a “Warning: Cannot use a scalar value as an array in …/query.php on line 2678″
The error is intermittent and I cannot figure out what to do to fix it. I tried adding your code in the top, but nothing seemed to change.
I looked at doug b’s option, but I would rather not edit the core wordpress files if I can avoid it.
Any ideas? Or other places I could look? I’m really stuck on this one…
June 9th, 2009 at 11:27 am
The error comes because you are trying to append an array value to a variable. PHP will automatically typecast a variable so you don’t have to declare it’s type but it wont re typecast.
so
will cause this error.
June 9th, 2009 at 11:28 am
Code example removed so here it is again. This will cause the same error.
$foo=0;//int
$foo[]=1;
June 25th, 2009 at 12:06 pm
I realized that I was getting that same error because I was dumping some form data into an array via:
$cart['data'] = $_POST;
Besause the $_ vars are of different types it would not allow me to add new varaibles to the $cart['data'] array, like so:
$cart['data']['total']
Then I changed the code to:
foreach($_POST as $k => $v)
$cart['data'][$k] = $v;
That made everything work just fine.
May 2nd, 2010 at 3:57 am
greetings there, i just saw your site via yahoo, and i would like to comment that you express exceptionally good via your web portal. i am very struck by the way that you compose, and the message is outstanding. anyways, i would also like to know whether you would love to exchange links with my blog? i will be certainly more than willing to reciprocate and put your link off in the blogroll. waiting for your response, i would like to convey my appreciation and enjoy your day!