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

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

WBOY
Release: 2016-06-06 20:27:22
Original
1328 people have browsed it

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

有什么区别?</code>
Copy after login
Copy after login

回复内容:

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

有什么区别?</code>
Copy after login
Copy after login

<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>
Copy after login

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

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

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template