In memory, the copy of the PHP simple variable is passed by value, and the copy of the array and object is passed by reference, which is implemented by reference.
Simple variables and objects are well understood:
<?PHP//copy of a simple variable$a= ' Human ';$b=$a;$b= ' Cat ';Var_dump($a);//string ' Human ' (length=5)//Copy of Objectclassa{}$a=NewA ();$b=$a;$b->name = ' Jack ';Var_dump($a);//object (A) [1] public ' name ' = = String ' Jack ' (length=4)
The memory address of the object $a and object $b points to the same place, that is, the actions for $a and $b are all directed to the same instance.
You can copy objects by value in PHP by using the Clone keyword:
<?phpclass a{public $name = ' Vardy ';}
$a = new A (), $b = clone $a; $b->name = ' Dee '; var_dump ($a); Object (A) [1] public ' name ' = = String ' Vardy ' (length=5) var_dump ($b); Object (A) [2] public ' name ' = = String ' Dee ' (length=3)
Array:
A copy of the <?php//array of $ A = [' Human ', ' orc ']; $b = $a; $b [0] = ' elve '; var_dump ($a);//Array (size=2)// 0 = String ' Human ' ( length=5)// 1 = String ' orc ' (length=3)
In this way, the copy of the array is passed by value.
An iterator to another example array:
<?php$a = [' Human ', ' Orc ', ' Elven ', ' Undead '];next ($a); $b = $a; At this point the pointer position is also copied var_dump (current ($a)) as the array is copied; String ' orc ' (length=3) var_dump (current ($b)); String ' orc ' (length=3)
When the array is copied, the pointer position of the array is copied as well.
(next:http://php.net/manual/zh/function.next.php
current:http://php.net/manual/zh/function.current.php)
<?php$a = [' Human ', ' Orc ', ' Elven ', ' Undead '];end ($a); next ($a); Array pointer is illegal, return false$b = $a; Var_dump (current ($a)); String ' Human ' (length=5) var_dump (current ($b)); Boolean false
When the position of the pointer is illegal when the array is copied, a different situation occurs when the current unit pointed to by the two array pointers is printed separately.
The reason is that after the array variable has been copied, the two variables in the first write operation, the pointer is initialized, that is, point to the first element, and the current method will also produce a write operation, so the $a array pointer is currently pointing to ' human ', and the $b the present method returns FALSE.
The following code, in the same vein, $b the first write operation occurred:
<?php$a = [' Human ', ' Orc ', ' Elven ', ' Undead '];end ($a), next ($a), $b = $a; $b [] = ' dwarf '; Var_dump (current ($a)); Boolean Falsevar_dump (current ($b)); String ' Human ' (length=5)
Parsing: A copy of an array is a value pass . PHP has a mechanism for managing memory called write-time replication (cow,copy on Write), which ensures that the values are copied between variables without wasting memory: when the value of one variable is copied to another variable, PHP does not use more memory for the copied value, instead, It updates the symbol table to show that the two variables have the same block of memory, so when the following code is executed, a new array is not created:
<?php$a = [' Human ', ' Orc ', ' Elven ', ' Undead ']; $b = $a;
When you modify $a or $b any of the replicas, PHP allocates the required memory for replication:
$b [] = ' dwarf ';
Reference:
Arrays of PHP base syntax and arrays pointers
Real understanding of referencing & in PHP-variable reference, function reference, object reference
Programming PHP 3rd Edition
Copies of PHP arrays are passed by value or by reference