Warnings Promoted to Errors in Array Handling
Learn about the warnings in arrays that are now considered errors in PHP 8.
We'll cover the following
A number of bad practices regarding arrays, allowed in PHP 7 and earlier versions, now throw an Error
. As discussed in the previous lesson, PHP 8 array error-handling changes serve to give you a more forceful response to the error situations we describe here. The ultimate goal of these enhancements is to nudge developers toward good coding practices.
Here is a brief list of array-handling warnings promoted to errors:
Cannot add an element to the array as the next element is already occupied
Cannot unset the offset in a non-array variable
Only array and Traversable types can be unpacked
Illegal offset types
Let’s now examine each of the error conditions on this list, one by one.
Next element already occupied
In order to illustrate one possible scenario where the next array element cannot be assigned as it’s already occupied, have a look at this simple code example:
Get hands-on with 1200+ tech skills courses.