How does this reference assignment work and what's the use of this thing? What kind of scenes are you using?

Source: Internet
Author: User
$data = Array (' A ', ' B ', ' C ');

foreach($data as $key=>$val) {           $val = &$data[$key];          }

Var_dump ($data);

Reply content:

$data = Array (' A ', ' B ', ' C ');

foreach($data as $key=>$val) {           $val = &$data[$key];          }

Var_dump ($data);

All the things you see on the display when the computer is powered on, are in memory

Memory, what is the structure of the specific, interested can go to understand the next;
Every unit is like a mailbox, it is a mailbox has its address, this is not difficult to understand it.

$data = array('a', 'b', 'c');foreach($data as $key=>$val) {    $val = &$data[$key];}

The above code logic is that the known $data array has three elements, the $data array of the last element of the address, put in $val this mailbox.
At this point we can assign operations to the $val, which can indirectly affect the last element of the $data array

$val = '间接影响';print $data[2];

The specific use of what scenario, can only tell you, when the need to indirectly affect something, use reference assignment.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.