$number=15;
$age=20;
$sum=12+"15";//$sum=27
代码如下 |
复制代码 |
$lang[]="php";
$lang[]="html";
$lang[]="css";
echo "$lang[0] ";
echo "$lang[1] ";
echo "$lang[2] ";
?>
|
2. There are two ways to create an array: variable assignment and function calling. Here we will talk about the former first.
Using the variable assignment method is very simple, just assign a value to an array variable directly.
Example:
The code is as follows
|
Copy code
代码如下 |
复制代码 |
$val1="hello";
$val2=& $val1;
$val2="goodby";
echo '$val1 is '.$val1."
";
echo '$val2 is '.$val2."
";
?>
$val1 is goodby
$val2 is goodby
|
|
$lang[]="php";
$lang[]="html";
$lang[]="css";
echo "$lang[0] ";
代码如下 |
复制代码 |
$a = array(
'a' => 'aa',
'b' => 'bb',
'c' => 'cc',
);
foreach( $a as &$v){
;
}
print_r($a);
foreach( $a as $v){
}
print_r($a);
|
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. Therefore, if multiple variables refer to the same content, modifying any one of them will be reflected in the remaining variables. Add an & symbol after the equal sign ($val2=& $val1) to complete the reference assignment or place the & symbol in front of the referenced variable ($val2= &$val1):
The code is as follows
|
Copy code
|
$val1="hello";
$val2=& $val1;
$val2="goodby";
echo '$val1 is '.$val1."
";
echo '$val2 is '.$val2."
";
$val1 is goodby
$val2 is goodby
Problem with foreach reference assignment
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);
-----------------------
If you think about it carefully, it is actually a simple reference problem. After the first foreach is completed, $v is actually a reference to $a['c']. During the loop, every assignment to $v will change $a[ 'c'], and the last assignment to $v was $v=$a['c'], $a['c'] was assigned $a['b'] last time, so it will The above situation will occur
http://www.bkjia.com/PHPjc/631537.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631537.htmlTechArticleAssignment in php is variable assignment and reference assignment. Let me introduce some basic usage to you. with the difference. 1. Value assignment: Copy the value of the assignment expression to the variable. ...
|
|
|