This article is shared with you about how PHP variables are defined and how PHP works, the content is very useful, and I hope to help small partners in need.
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 variable: 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//defines variable B and assigns the value of a variable to B, where there are two variables ($a and $b) pointing to the range. $b = $a; Var_dump ($b); int 100//modifies the value of the $ A variable, and the PHP variable has the property of copy on write, 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 10var_dump ($b); int 100//------------------------------------------------------------------$c =: $d = & $c; $c = 10;var_dump ($ c); int 10var_dump ($d); int 10//When a reference is directed, the PHP variable does not have a cow attribute and points to the same memory area. * unset () can only eliminate the variable reference, cannot delete its memory allocation space//------------------------------------------------------------------/* Example: 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 ', = = ' = ' 1] $d = & $val [2]=> ' C ' $ data = [' B ', ' C ', ' C ']; */