Home > Backend Development > PHP Tutorial > 新人求教,这个引用赋值是怎么完成的而且这东西有什么用啊?用在啥场景中?

新人求教,这个引用赋值是怎么完成的而且这东西有什么用啊?用在啥场景中?

WBOY
Release: 2016-06-06 20:22:53
Original
1161 people have browsed it

$data = array('a', 'b', 'c');

<code>foreach($data as $key=>$val) {
       
    $val = &$data[$key];   
    
   
}</code>
Copy after login
Copy after login

var_dump($data);

回复内容:

$data = array('a', 'b', 'c');

<code>foreach($data as $key=>$val) {
       
    $val = &$data[$key];   
    
   
}</code>
Copy after login
Copy after login

var_dump($data);

电脑开机后,你在显示屏看到的所有东西,都存在内存中

内存,具体是个什么结构,有兴趣可以去了解下;
它每个单位就是好像一个邮箱,是个邮箱都有它的地址,这个不难理解吧。

<code>$data = array('a', 'b', 'c');

foreach($data as $key=>$val) {

    $val = &$data[$key];
}</code>
Copy after login

上面这段代码逻辑是,已知$data数组有三个元素,把$data数组里最后一个元素的地址,放到$val这个邮箱中。
这时我们可以对$val进行赋值操作,间接的可以影响到$data数组的最后一个元素

<code>$val = '间接影响';
print $data[2];
</code>
Copy after login

具体用什么场景,只能和你说,当需要间接影响到某个东西的时候使用引用赋值。

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