PHP stops with "[] operator not supported for strings" when you append to a variable
Fatal error: [] operator not supported for stringsShort answer
PHP 7.1 and later stop with a fatal error when you write $var[] = $value and $var is a string. Older versions turned the string into an array without telling you. Find the variable on the failing line, see where it got its string value, and start it as [] instead of an empty string.
Fix it fast
1Find out which cause you have
Open the file and line from the error. Just above that line, print the type of the variable you append to. If it says string, one of the causes below applies.
var_dump(gettype($rows)); $rows[] = $item; // the line from the error message2Match what you see, then jump to its fix
- 1
Code like $rows = ''; near the top, then $rows[] = ... in a loopThe variable was started as an empty stringFix - 2
The error shows up only for some requests, or after you read the value from a form, a query string or a config fileThe variable arrives from outside as a stringFix - 3
The page worked on PHP 7.0 or older and broke after moving to a newer PHPOld code that relied on the silent conversionFix
- 1
What does the error look like?
Fatal error: [] operator not supported for stringsApplying the empty index operator to a string throws a fatal error
Cause 1 of 3
The variable was started as an empty string
1Check
Search the file for the place the variable first gets a value. An empty string looks harmless, but it is a string, not an empty array.
grep -n '\$rows' src/report.php2Fix
Start the variable as an empty array. The manual says directly that a variable should be set up with a plain assignment rather than relying on it being created later.
$rows = [];
foreach ($items as $item) {
$rows[] = $item;
}3Verify
Run the script again. The fatal error is gone and the variable holds an array.
php -r '$rows = []; $rows[] = 1; var_dump($rows);'Confirmed by php.net
Cause 2 of 3
The variable arrives from outside as a string
1Check
The manual warns that if the variable already holds a value such as a string from a request variable, that value stays in place and [] then acts as the string access operator.
var_dump($_POST['tags'] ?? null);2Fix
Do not append to the incoming value. Build your own array from it, and test with is_array() first when the input can be either shape.
$incoming = $_POST['tags'] ?? [];
$tags = is_array($incoming) ? $incoming : [$incoming];
$tags[] = 'extra';3Verify
Send the request once as a string and once as an array. Both should run without the fatal error.
php -l src/tags.phpConfirmed by php.net
Cause 3 of 3
Old code that relied on the silent conversion
1Check
The PHP 7.1 migration notes say that applying the empty index operator to a string now throws a fatal error, where before it quietly converted the string to an array. Every append on a string in your code base is affected.
php -v && grep -rn --include=*.php "= ''" src/ | head -302Fix
Go through the matches and change each variable that is later appended to into []. Do not keep the old behaviour by pinning an old PHP version.
// before
$list = '';
$list[] = $x;
// after
$list = [];
$list[] = $x;3Verify
Run the test suite or load each page once on the new PHP version.
php -l src/list.phpConfirmed by php.net
How do I stop it coming back?
- Start every list variable with [] and every text variable with '', never mix the two.
- Validate request input before use and cast or wrap it into the shape your code expects.
- Run your tests on the same PHP major version that production uses.
- Use a static analysis tool in CI so a string appended like an array is reported before deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix