This question specifically relates to my experience with PHP, but there’s no reason why it shouldn’t apply to other languages.
Having set ERROR_REPORTING(E_ALL); during testing, I noticed that I had some notices, such as Notice: Undefined offset: 0 in ChrisW/public_html/admin.php on line 122. This comes about as I store warnings about a form (unfilled values, invalid values, etc) in an array, and then access them:
echo $errorsArray[0] . '<label for="name" id="name-label">Your Name</label>
<input id="name" name="name" value="' . $name . '" />';
with my errors array being $errorsArray = array($nameError, $emailError); in my validation function. The 1st time the page is loaded, I show the form:
if(count($_POST) == 0)
{
$name="";
$email="";
$errorsArray = array();
form_display($name, $number, $errorsArray);
}
In an ideal world:
- does it matter about the notices?
- should I initialise the array by doing
$errorsArray("","");, or - Should I check if the array is empty when I reference it (such as by changing
echo $errorsArray[0]toecho (count($errorsArray[0])==0 ? "" : $errorsArray[0])?
Hopefully, there’ll be a fairly black and white answer – I don’t want to cause a long discussion!
Production code SHOULD NOT raise notices. This causes scroll blindness – you get used to having a lot of meaningless messages, so you don’t notice the important one (until it’s too late).
Random comments: