PHP creates new instance when pushing element

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

Need Your Help

How does jstl's sql tag work?

java jsp web-applications jstl

I'm using the following code to query a database from my jsp, but I'd like to know more about what's happening behind the scenes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.