The problem has nothing to do with the php version. Either the code you found was always incorrect or your copying of it introduced errors. This is the problem with seeing and repeating code found on the web. There’s no learning going on, so, when it doesn’t work, you don’t have the knowledge to find what’s wrong with it and fix it or use it later when writing new code.
See the ‘Creating/modifying with square bracket syntax’ section at this link to the php documentation - PHP: Arrays - Manual
The correct syntax for adding an element to an array is -
$array_var[] = expression;
Where expression is anything that results in a value, such as a php string.
The posted code has some lines that are correct and some lines without the [] part of the syntax.