Assignment in PHP is assigned to a variable assignment and reference assignment, below I will give you some of their basic usage and differences.
1, Value assignment: Copies the value of an assignment expression to a variable. Example:
| The code is as follows |
Copy Code |
$number = 15; $age = 20; $sum "=12+";//$sum =27 |
2. Create the array has variable assignment and call function two methods, here first of the former.
It is very simple to assign a value to an array variable by using the variable assignment method.
Instance:
| The code is as follows |
Copy Code |
$lang []= "PHP"; $lang []= "HTML"; $lang []= "CSS"; echo "$lang [0] "; echo "$lang [1] "; echo "$lang [2] "; ?> |
Array contents generated by three assignment statements:
0=>php
1=>html
2=>css
3. Reference assignment: The variable created is the same as the content referenced by another variable. So if more than one variable references the same content, modifying any of the variables will be reflected on the remaining variables. Add a & symbol after the equals sign ($val 2=& $val 1) To complete the reference assignment or put the & symbol in front of the referenced variable ($val 2= & $val 1):
| The code is as follows |
Copy Code |
$val 1= "Hello"; $val 2=& $val 1; $val 2= "Goodby"; Echo ' $val 1 is '. $val 1. " "; Echo ' $val 2 is '. $val 2. " "; ?> $val 1 is Goodby $val 2 is Goodby |
Problems with the assignment of a foreach reference
Code:
| The code is as follows |
Copy Code |
$a = Array ( ' A ' = ' AA ', ' B ' = ' BB ', ' C ' = ' cc ', ); foreach ($a as & $v) { ; } Print_r ($a); foreach ($a as $v) {
} Print_r ($a); |
----------------------
Think carefully, in fact, is a simple reference problem, the first foreach is finished, $v is actually $a[' C ') reference, the loop, each time the assignment to $v will change the value of $a[' C '], and the last assignment to $v is $v= $a [' C '], $a [' C '] in the last assignment was $a [' B '], so this will happen
http://www.bkjia.com/PHPjc/631537.html www.bkjia.com true http://www.bkjia.com/PHPjc/631537.html techarticle Assignment in PHP is assigned to a variable assignment and reference assignment, below I will give you some of their basic usage and differences. 1, Value assignment: Copies the value of an assignment expression to a variable. ...