Why does PHP create a new array when I push an element?
$a = array();
$b = $a;
$b[] = "Hello!";
echo count($a);
echo count($b);
Here you would expect the count for $a and $b to be equal, but they are not.
By default, PHP is copying values when assigning them. If you want a reference, you can use the & operator:
$a = array();
$b = &$a;
$b[] = "Hello!";
echo count($a); // prints 1
echo count($b); // prints 1
This is because $a and $b are two separate variables, when you assign $b = $a you are only copying the contents of $a to $b, not making them both point to the same array.
To do this, you need to pass by references instead:
$b = &$a;
In this case, $b points to the reference to $a, which means they both point to the same array, any change to one will change on both.
Arrayin javascript is anObject, but in php it is not.