Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP stops with "[] operator not supported for strings" when you append to a variable

ErrorFatal error: [] operator not supported for strings

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 message
  2. 2Match what you see, then jump to its fix

    1. 1Code like $rows = ''; near the top, then $rows[] = ... in a loopThe variable was started as an empty stringFix
    2. 2The 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. 3The page worked on PHP 7.0 or older and broke after moving to a newer PHPOld code that relied on the silent conversionFix

What does the error look like?

  • Fatal error: [] operator not supported for strings
  • Applying 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.php

2Fix

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

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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 -30

2Fix

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.