Article updated: 11 Dec 2017
PHP notices are “soft errors” in PHP programming language. If you enable error logging (debug) in php.ini configuration file, they will pop-up in your logs from time-to-time during various development stages. They should not, however, be present in full production application (if code quality was somewhere among your goals).
But, what do they mean? Should you be worried about them? Can they mean something else? Read this article to find out.
PHP NOTICES EXPLAINED
One of the most frequent PHP notice types are probably found in arrays, where you may get something similar to this:
<?php echo array[name]; ?>
And when you execute the above code, you will get the following notice:
PHP Notice: Use of undefined constant name – assumed ‘name’ in /path/…
The above notice means that your array key element was specified/called without quote escaping, e.g.:
proper syntax is:
<?php echo array[‘name’]; ?>
or with double-quotes:
<?php echo array[“name”]; ?>
The above is indeed a very benign notice and quite simple to fix.
That was easy, right? But, there are situations when a PHP notice could mean something much more serious is going on inside your code, in a place you may least suspect. Now, it is impossible to give a proper example for it, without actual context (e.g. working on a large CMS system such as WordPress).
PHP Notice: array to string conversion
The above is also one of the frequent ones, and relatively easy to solve. The problem is caused by user error e.g. when you try to use a built-in PHP function or a custom function which expects string as input variable, but you supply an array(), instead.
For example, let’s have an array():
<?php $array = array(a,b,c,d,e); ?>
We have an array with 5 elements, and if we try to “echo” it’s content with code below:
<?php echo $array; ?>
It will throw a notice inside your PHP error log, complaining that you try to echo an array of elements. Remember, echo can print only single variable (or multiple concatenated variables into single string), and array() element can contain millions of them. PHP does not internally have any mechanism to automatically convert array into strings and echo all of them (it would be useful in a way, but very impractical on the other hand).
To print a specific value from array (e.g. letter c) you need to specify it’s key like this:
<?php echo $array[2]; ?>
or loop through array, alternatively.
Sometimes, you may try to work with or echo an undefined array key/element:
<?php echo $array[‘city’]; ?>
and a result will be this:
PHP Notice: Undefined index: city
The above notice type is also common, and your first idea for a quick solution/fix could be something like this:
<? php if (isset($array[‘city’])) { echo $array[‘city’]; } ?>
And gone! Notice will not bother you ever again.
Alternatively, you may define a new $city variable as empty string just before you echo it (echo = can be any other variable processing function, just to make this clear – we use echo here for simple illustration purposes):
<?php
$city = ”;
if (isset($array[‘city’])) { $city = $array[‘city’]; }
echo $city;
?>
Now it would be a good time to evaluate your code and make 100% sure WHAT and WHY is causing the above notice!? Is it merely a convention issue, undefined variable/array element, or something else – originating much earlier in the code execution flow?
We had countless situations like above, where the so-called “fix” with isset() would simply hide the problem under the carpet, and leave a much much bigger issue hidden from developer’s eyes.
For example, you need to ask yourself: is it normal to have a non-defined value for city key? Is it expected? If not – why is it happening? You should definitely pay a visit to your database and see if everything checks there, as well. Most probably, the values are gonna be blank, but you need to investigate further WHY, if that is an unexpected/unnatural behavior.
PHP Notice: Constant ABS_PATH already defined
Very easy and benign notice, you have somewhere in your code a duplicate / multiple definitions of already defined constant.
PHP Notice: unserialize(): Unexpected end of serialized data
PHP Notice: unserialize(): Error at offset X of Y bytes
The above notice indicates that your serialized data syntax is corrupted in some way (most probably caused by manual human tampering/editing), and you need to fix it manually. Or re-generate new serialized data, if possible.
Recently, we had a very peculiar set of notices repeating on a daily basis in our logs that we couldn’t identify why they were happening:
PHP Notice: Undefined index: item_id
As ordinary PHP reporting was not telling us much, we constructed a special monitoring function that logged everything, from server requests, to complete set of variables in specific function(s). This way we were able to catch the reason why they were happening and why they were triggered in the first place.
As it turned out in the end, they were triggered by a scraping / indexing / SEO bot, which was continuously hitting/visiting pages of items no longer available. We had to ban that bot in the robots.txt file, but also blocked it’s UA (user agent) on a server level.
Now, here’s the thing: the inner logic of above functions never expected empty array with undefined item_id key/value, but on the other hand, it actually helped us track the nasty bot, otherwise it would went completely unnoticed and undetected.
So, sometimes, it might be a good idea to make your code a bit “stupid” and less smart, in order to detect unusual conditions.
CONCLUSION
PHP Notices are usually benign errors, which do not prevent code to run / execute. However, if your goal is to become a great programmer, you must pay close attention to them, especially when you work in complex CMS environment, where many unexpected things can happen. Sometimes, notices are a true life-savers signaling you the more serious underlying issues you might not be aware of at all! This is especially true when working with database(s) and values are retrieved from or stored into it, if you get a blank variable it may mean that PHP simply doesn’t “like” it, or it may mean that your logic for storing data in the first place needs some improvements and further checking.
Comments
Post A Comment