Home>Article>Backend Development> What are php reference variables? Example explanation of php reference variables

What are php reference variables? Example explanation of php reference variables

不言
不言 Original
2018-09-08 15:47:56 2393browse

What are php reference variables? How to define reference variables? This article will briefly introduce the basic concepts and working methods of reference variables in PHP from the memory space, as well as a common example.

Concept

What is a reference variable? In PHP, what symbols are used to define reference variables?

Different names access the same variable content, use & to express .

COW(copy on write)

A common method of memory optimization, this method is also used in PHP to optimize memory.

Copy on write, that is, only when one or more variables are written, a copy of the memory will be copied and its contents will be modified.

Use memory_get_usage() in PHP to observe memory changes

Demonstration

Unused & (reference variable)

$a = range(0,1000); var_dump(memory_get_usage()); $b = $a;` var_dump(memory_get_usage()); $a = range(0,1000); var_dump(memory_get_usage());

Running results:

What are php reference variables? Example explanation of php reference variables

There is not much difference in the memory between the first and second times, but there is a big difference in the third time

Using &

$a = range(0,1000); var_dump(memory_get_usage()); $b = &$a; var_dump(memory_get_usage()); $a = range(0,1000); var_dump(memory_get_usage());

Running results:

What are php reference variables? Example explanation of php reference variables

Using reference to pass value memory Parsing analysis:

What are php reference variables? Example explanation of php reference variables

##$aoccupies A memory space in the memory when assigned,$b=&$ When a$bpoints to the same memory space, when$achanges, the memory space occupied by$bwill follow$achanges

unset

unset()will only dereference and not destroy the space

$a=1; $b=&$a; unset($b); echo $a;
Output result:

1

Memory space analysis:


What are php reference variables? Example explanation of php reference variables

#$a is assigned $b=&$a and then $a and $b go straight to the same memory space , when unset($b) cancels the reference of $b to $a, so that $b no longer points to the memory space of $a

Digression

The object itself is passed by reference

class Person { public $name="zhangsan"; } $p1 =new Person; xdebug_debug_zval('p1'); $p2 =$p1; xdebug_debug_zval('p1'); $p2->name="lisi"; xdebug_debug_zval('p1');
Running results:

What are php reference variables? Example explanation of php reference variables

Result analysis:

What are php reference variables? Example explanation of php reference variables

After the object is instantiated and passed by reference
$p1 $p2 always points to the same memory space
Case

 $val) { $val = &$data[$key]; } var_dump($data);

Write as above The output of the program?

When the program is running, what is the value of the variable $data after each loop ends?
After the program execution is completed, what is the value of the variable $data?

Memory changes and analysis:

What are php reference variables? Example explanation of php reference variables

Related recommendations:

php variable reference and object reference Detailed introduction_PHP tutorial

phpDetailed introduction to variable reference and object reference

The above is the detailed content of What are php reference variables? Example explanation of php reference variables. For more information, please follow other related articles on the PHP Chinese website!

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