Home >Backend Development >PHP Tutorial >看别人的PHP代码有一点不明白

看别人的PHP代码有一点不明白

WBOY
WBOYOriginal
2016-06-06 20:27:221326browse

<code>foreach ($data as &$e) {}
和
foreach ($data as $e) {}

有什么区别?</code>

回复内容:

<code>foreach ($data as &$e) {}
和
foreach ($data as $e) {}

有什么区别?</code>

<code class="php">$data = [1,2,3];
foreach($data as &$e){
    $e+=1;
}
var_dump($data);//[2,3,4]
unset($e);

$data = [1,2,3];
foreach($data as $e){
    $e+=1;
}
var_dump($data);//[1,2,3]</code>

楼上很清晰,再给两个链接:

http://php.net/manual/en/control-structures.foreach.php

http://php.net/manual/en/language.references.php

一个是引用,作用于原值本身;一个是使用,不会更改原值。

不加&的话,就相当于内部复制了一个变量去使用,不会修改原本的值,地址也不会有改变。

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