Junior — Middle
Millistele andmetüüpidele on keelatud määrata indeksväärtust?
sobes.tech AI
Vastus AI-lt
In PHP, assigning an index value (for example, when accessing an array via square brackets) is prohibited for the following data types:
- Scalar types (int, float, string, bool) — they are not arrays or objects, so you cannot do something like
$intVar[0] = 'a';. - NULL — assigning an index is not allowed because NULL is not an array or an object.
Index assignment is only possible for:
- Arrays
- Objects implementing the ArrayAccess interface
Example:
$arr = [];
$arr[0] = 'value'; // correct
$intVar = 10;
$intVar[0] = 'a'; // error