arrays - PHP creates new instance when pushing element -


why php create new array when push element?

$a = array(); $b = $a;  $b[] = "hello!"; echo count($a); echo count($b); 

here expect count $a , $b equal, not.

by default, php copying values when assigning them. if want reference, can use & operator:

$a = array(); $b = &$a;  $b[] = "hello!"; echo count($a);  // prints 1 echo count($b);  // prints 1 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -