Fatal error: [] operator not supported for strings
This error is usually caused when you are trying to use the square brackets [] to access a character in a string, but this is not supported in PHP.
This error occurs in PHP 8.0+ when you try to use the square brackets [] to append data to a string. In PHP, strings are immutable, and the [] operator is strictly reserved for arrays.
For example, the following code will trigger this error:
Solving Fatal error: [] operator not supported for strings error in PHP
<?php
$str = 'Hello';
$str[] = ' world'; // Fatal error: [] operator not supported for stringsTo append to a string, use the concatenation assignment operator .=:
Example of appending to a string in PHP
<?php
$str = 'Hello';
$str .= ' world'; // Correctly appends to the string
echo $str; // prints "Hello world"If you specifically need to use array syntax on a string, cast it to an array first:
Example of converting a string to an array in PHP
<?php
$str = 'Hello';
$chars = (array) $str;
$chars[] = ' world'; // Works as expectedFor multibyte strings, use mb_str_split() to convert to an array of characters, or use mb_substr() for safe character extraction.