Fatal error: [] operator not supported for strings
Such behavior is described in Migrating from PHP 7.0.x to PHP 7.1.x/
The empty index operator is not supported for strings anymore Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.
In my case it was a mere initialization. I fixed it by replacing $foo=''
with $foo=[]
.
$foo='';
$foo[]='test';
print_r($foo);
You have probably defined $name
, $date
, $text
or $date2
to be a string, like:
$name = 'String';
Then if you treat it like an array it will give that fatal error:
$name[] = 'new value'; // fatal error
To solve your problem just add the following code at the beginning of the loop:
$name = array();
$date = array();
$text = array();
$date2 = array();
This will reset their value to array and then you'll able to use them as arrays.
You get this error when attempting to use the short array push syntax on a string.
For example, this
$foo = 'foo';
$foo[] = 'bar'; // ERROR!
I'd hazard a guess that one or more of your $name
, $date
, $text
or $date2
variables has been initialised as a string.
Edit: Looking again at your question, it looks like you don't actually want to use them as arrays as you're treating them as strings further down.
If so, change your assignments to
$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];
It seems there are some issues with PHP 7 and code using the empty-index array push syntax.
To make it clear, these work fine in PHP 7+
$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry
$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry
What does not work is attempting to use empty-index push on any variable declared as a string, number, object, etc, ie
$declaredAsString = '';
$declaredAsString[] = 'value';
$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';
$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';
All result in a fatal error.