Home  >  Article  >  Backend Development  >  谁能说说这串PHP代码的运行原理?

谁能说说这串PHP代码的运行原理?

WBOY
WBOYOriginal
2016-06-06 20:25:081100browse

$array=array(1,2,3,4,5);
foreach($array as &$v){

}

$v=0;
var_dump($array);
/**结果
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
&int(0)
}
*/

回复内容:

$array=array(1,2,3,4,5);
foreach($array as &$v){

}

$v=0;
var_dump($array);
/**结果
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
&int(0)
}
*/

不就是引用嘛。
array的最后一个值与$v指向同一个地址。

如果你 $array[4] = 999; 那么 $v也是999.

参考这个 => http://segmentfault.com/q/1010000002676308

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn