When to use []

This is a shortcut for array_push. It’s easier and even better if you add one element to the array.

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

If you make a mistake and you try to use this on something else than an array, you will have a warning both case. In the doc, you have something else

Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
$arr = 5;
array_push($arr, 4);

// PHP Warning:  array_push() expects parameter 1 to be array, integer given

$arr = 5;
$arr[] = 4;

// PHP Warning:  Cannot use a scalar value as an array

// PHP 5.6.7-1+deb.sury.org~trusty+1 (cli) (built: Mar 24 2015 11:21:10) 
// Copyright (c) 1997-2015 The PHP Group
// Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
//     with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
//     with Xdebug v2.3.1, Copyright (c) 2002-2015, by Derick Rethans

When not to use []

This is a list of when not to use [] and will result in this error

$a = array();
$b = $a[];
///////////////////////////////////
$a = array();
isset($a[]);
///////////////////////////////////
function test($var) { }
test($arr[]);
///////////////////////////////////
$a = array();	
foreach($a[] as $b) {
}
///////////////////////////////////
$a = array();	
foreach($a[]['test'] as $b) {
}
///////////////////////////////////
$a = array();
foreach($a['test'][] as $b) {
}
///////////////////////////////////

You can find more example here

The problem i had

I came accros some code trying to use variables variables and []. Since the precedence is kind of blurry here, i got this error in this particular scenario.

$test = 'var';
$name = 'test' . $test;

$testvar = array();
$$name[] = 3; // Add something to $testvar

// PHP Fatal error:  Cannot use [] for reading

The solution

1- Use array_push : array_push($$name, 3);
2- Use {} to force precedence : ${$name}[] = 3;

What i have found

I ran it over 3v4l and it seems to work now on php7@20140901 – 20150501