Fatal error: Uncaught Error: [] operator not supported for strings - PHP 7.1

Date posted: 2018-03-02
Last updated: 2026-10-04

With PHP 7.1, some Joomla, WordPress and Drupal components fail with "[] operator not supported for strings". Here's how to fix this code for PHP 7.1+.

With PHP 7.1, some PHP web applications fail because of deprecated code usage. This may result in an error message like [] operator not supported for strings for various Joomla, WordPress and Drupal components. Here's how to fix this code for PHP 7.1+. I'll use Joomla as an example in this short post; the same error and fix may apply to other CMS systems like WordPress and Drupal.

Since PHP 7.1, using the empty index operator on a string ($str[] = $x) throws a fatal error instead of silently converting the string to an array. Find the failing line with your CMS's debug mode and initialize the variable as an array first: $data = [];.

I originally wrote this post in 2018, for PHP 7.1. That PHP version is long end of life, but this error and its fix are the same in PHP 8.

Find the error with Joomla debug mode

First you have to enable Joomla's debug mode, which shows you where the error is happening. Open up configuration.php and set public $debug to 1.

If you refresh the page now, you'll see Joomla's error page, telling you about the Fatal error: Uncaught Error:

Fatal error: Uncaught Error: [] operator not supported for strings D:\www\example.com\www\components\com_layer_slider\base\includes\slider_markup_init.php:98

Fix the [] operator not supported for strings error

OK, so we now know where the error is: it's on line 98 in com_layer_slider's base\includes\slider_markup_init.php file. In this case, the failing code is:

$data[] = '<script type="text/javascript">' . NL;

You can fix this PHP error by first initializing $data as an array:

$data = [];
$data[] = '<script type="text/javascript">' . NL;

A second method is to use the older style using array():

$data = array();
$data[] = '<script type="text/javascript">' . NL;

Let's hope not too many Joomla components, plugins and extensions use code like this. Not to mention other CMS systems like ExpressionEngine, WordPress, BuddyPress, PrestaShop...

WordPress Betheme theme

Update 2018-03-03: the WordPress premium Betheme theme by Muffin group needs a fix in the file:

wp-content\themes\betheme\functions\builder\front.php

On line 150:

// $section_style         = '';
$section_style         = [];

WordPress Revolution Slider

Older versions of the WordPress plugin Revolution Slider by ThemePunch have this same deprecated PHP code, and throw an error:

Uncaught Error: [] operator not supported for strings in wp-content\plugins\revslider\includes\framework\base-admin.class.php:69

To fix this, add a line before line 69 that creates the variable as an array when it isn't one yet. Only do this when it is not an array already, otherwise you throw away the meta boxes that were added earlier:

if ( ! is_array( self::$arrMetaBoxes ) ) self::$arrMetaBoxes = []; # add this line to create the variable as an array
self::$arrMetaBoxes[] = $box;

A PHP upgrade breaking your website is nothing new: the PHP 5.6 default_charset change may break HTML output too.

I write these posts in my spare time, based on real problems from my day job as a sysadmin. If this one saved you some debugging time, a small donation is much appreciated. Thanks! 🙏

Leave a Comment