This error means that you are trying to use a null (or a non-existent value) as array. Which definitely means that logic of your program is broken.
First of all you must understand that every error message PHP generates is meant to help you, and to make your code cleaner and less error-prone. When your code contains a bug, your best bet is to fix it, not to brush it off. Sadly, the accepted answer only elaborates on the latter. But you should really consider doing the former.
Every error message helps you to find a bug in the code. It means that errors shouldn't be intentional or "habitual". This way you won't have to silence them, and every error would mean that your code indeed encountered a bug.
Like in your case. It just makes not sense to access a null or a non-existent variable as s though it's array. You can only use array offset on arrays, special type of objects and (using only numeric indices) on strings. But if you get a null value where array is expected, it means that the data flow in your program is broken. And you need to fix it.
Like in your case. Definitely, $cOTLdata
contains null
while your code expects it to be an array. Therefore, you need to fix the code that assigns value to $cOTLdata.
So, as a rule, just make sure that every function that should return an array, returns an array, or any variable that should contain an array, contains an array. And you won't see this error message again. Unless, due to some mistake, your code will return a non-array value unexpectedly. And this error message will help you to pinpoint this problem.
When things are not under your control, like, you need to get an array from an outside variable, do a validation first. For example, in case you are expecting an array from a form, that is obligatory, validate it and return an error, like
if (!$isset($_POST['options']) || !is_array($_POST['options'])) {
// inform the user ad stop execution
} else {
$options = $_POST['options'];
}
// now you can safely use count() on $options
In case the array is optional, you may initialize it as empty array:
if (!$isset($_POST['options'])) {
$options = [];
} elseif (!is_array($_POST['options'])
// inform the user ad stop execution
} else {
$options = $_POST['options'];
}
// now you can safely use count() on $options
And only as a last resort you may silence this error, using is_null()
or isset()
But you shouldn't just mindlessly use it as a generic solution. Using isset()
won't fix the problem. It will just sweep it under the rug, effectively action as error suppression operator, so when some your function will start returning incorrect value, you will never get an helpful error message that explains, why your program suddenly stopped working.