1. Variable definition:
It is common to learn that a variable represents a storage space and an "identifier" for its data.
Variable name point to variable value
In more depth, a piece of area where the variable points to memory
2, the variable working principle, through the drawing analysis method--memory space
<?php
PHP reference variables: Different variable names point to the same address.
Defines a variable a, at which time the memory opens up an area $a points to that area.
$a = 100;
Var_dump ($a); int 100
Define the variable B and assign the value of the A variable to B, where there are two variables ($a and $b) pointing at that area.
$b = $a;
Var_dump ($b); int 100
The value of the $ A variable is modified, and the PHP variable has the copy on write feature, so the region value pointed to by a is copied and rewritten, and A and B point to different regions respectively.
$a = 10;
Var_dump ($a); int 10
Var_dump ($b); int 100
//------------------------------------------------------------------
$c = 100;
$d = & $c;
$c = 10;
Var_dump ($c); int 10
Var_dump ($d); int 10
When a reference is directed, the PHP variable does not have the cow attribute and points to the same memory area.
* unset () can only eliminate the reference of a variable and cannot delete its memory allocation space
//------------------------------------------------------------------
/*
Examples:
Write the output of the following program:
<?php
$data = [' A ', ' B ', ' C '];
foreach ($data as $key = = $val) {
$val = & $data [$key];
}
Q: What is the value of the final $data?
*/
$data = [' A ', ' B ', ' C '];
foreach ($data as $key = = $val) {
$val = & $data [$key];
Print_r ($data);
}
/*
1. $key = 0,
$val = ' A ',
$val = & $d [0] = = ' a '
$data = [' A ', ' B ', ' C '];
2. $key = 1,
$val = ' B ', = = $d [0]
$val = & $d [1] = ' B '
$data = [' B ', ' B ', ' C '];
1. $key = 2,
$val = ' c ', = = $d [1]
$val = & $d [2]=> ' C '
$data = [' B ', ' C ', ' C '];
*/
Above from code https://www.cnblogs.com/luma/p/7465388.html#undefined
PHP variable definition and how it works