Sobes.tech
Junior — Middle

To which data types is assigning an index value prohibited?

sobes.tech AI

Answer from AI

In PHP, it is forbidden to assign index values (for example, when accessing an array through square brackets) to 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 — you cannot assign an index because NULL is not an array or 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