“[] operator not supported for strings” Error on Joomla sites

A regular client of ours contacted us this morning telling us that after updating PHP from 5.6 to 7.2 on his server, he saw the following fatal error on the homepage of his company website:

[] operator not supported for strings.

We did see this problem before on another client’s Joomla site, and it was caused when a PHP variable is assigned to a string, and then used as an array. In previous PHP versions (versions less than PHP 7.2), this mixed type shenanigan was acceptable, but as of PHP 7.2, this resulted in a fatal error. For example, the following code…

$arrResult = '';
for ($i = 0; $i < 5; $i++)
	$arrResult[] = $i;

…was OK for PHP 5.6, but yields a fatal error in PHP 7.2+. To comply with PHP 7.2, the above code should be:

$arrResult = array();
for ($i = 0; $i < 5; $i++)
	$arrResult[] = $i;

As developers, we’re always happy for strict coding practices. Having said that, we acknowledge that the above can cause many scripts to break under PHP 7.2.

So, how did we fix the problem?

We traced the issue to a file in a 3rd party system plugin that had the problem described above (a variable defined as an empty string and later used as an array). We fixed the problem by just assigning the variable to an empty array (instead of an empty string), and that fixed the problem!

Is the Joomla core affected by this change in PHP 7.2?

No – the PHP core developers ensured very long ago that the Joomla core was compliant with this change in PHP 7.2. If you see this problem on your website, then it is definitely caused by a 3rd party extension and not by the Joomla core.

We hope that you found this post useful and that it helped you fix the problem on your Joomla website. If you need help with the fix, then don’t be shy to contact us. We always love to work with new (and, of course, existing) clients, our rates are always right, our solutions are always clean, and we are the friendliest developers on this planet!

No comments yet.

Leave a comment