PHP variable definition and how it works

Source: Internet
Author: User

1. Variable definition:

It is common to learn that a variable represents a storage space and an "identifier" for its data.

Variable name point to variable value

In more depth, a piece of area where the variable points to memory

2, the variable working principle, through the drawing analysis method--memory space

<?php
PHP reference variables: Different variable names point to the same address.
Defines a variable a, at which time the memory opens up an area $a points to that area.
$a = 100;
Var_dump ($a); int 100
Define the variable B and assign the value of the A variable to B, where there are two variables ($a and $b) pointing at that area.
$b = $a;
Var_dump ($b); int 100
The value of the $ A variable is modified, and the PHP variable has the copy on write feature, so the region value pointed to by a is copied and rewritten, and A and B point to different regions respectively.
$a = 10;
Var_dump ($a); int 10
Var_dump ($b); int 100

//------------------------------------------------------------------

$c = 100;
$d = & $c;
$c = 10;
Var_dump ($c); int 10
Var_dump ($d); int 10
When a reference is directed, the PHP variable does not have the cow attribute and points to the same memory area.

* unset () can only eliminate the reference of a variable and cannot delete its memory allocation space

//------------------------------------------------------------------

/*
Examples:
Write the output of the following program:
<?php
$data = [' A ', ' B ', ' C '];
foreach ($data as $key = = $val) {
$val = & $data [$key];
}
Q: What is the value of the final $data?
*/
$data = [' A ', ' B ', ' C '];
foreach ($data as $key = = $val) {
$val = & $data [$key];
Print_r ($data);
}

/*
1. $key = 0,
$val = ' A ',
$val = & $d [0] = = ' a '
$data = [' A ', ' B ', ' C '];

2. $key = 1,
$val = ' B ', = = $d [0]
$val = & $d [1] = ' B '
$data = [' B ', ' B ', ' C '];

1. $key = 2,
$val = ' c ', = = $d [1]
$val = & $d [2]=> ' C '
$data = [' B ', ' C ', ' C '];
*/

Above from code https://www.cnblogs.com/luma/p/7465388.html#undefined

PHP variable definition and how it works

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.