This error means that you are trying to use null as array. What your code trying to do here is like null['char_data'], which obviously makes no sense.
There could be two reasons for this error to appear:
- Some function may return either a meaningful array or
null.
- A bug in your code.
When null value is expected
There are cases when null is a legitimate value. For example, a function may return either a meaningful array or null, as it happens with PHP mysqli API. In this case this return value has to be tested first, like this:
$row = $stmt->fetch_assoc();
if ($row !== null) { // the record was found and can be worked with
echo $row['name'];
}
Another case is when you are dealing with outside variable or any other situation when things are not under your control. In this case, you have to validate that input data first.
For example, in case you are expecting an array from a form, and this array is obligatory, validate it and return an error, like
if (!isset($_POST['options']) || !is_array($_POST['options'])) {
// inform the user and 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
In case it's a bug in your code
You have to fix it.
You must understand that using isset() won't fix the problem. It will just sweep it under the rug, effectively acting as the error suppression operator, so when some your function will start returning incorrect value, you will never get a helpful error message that would have explained, why your program suddenly stopped working.
$len = count($cOTLdata['char_data'] ?? []);