Variable Reference Passing by Value Study Notes in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:50:23
Original
751 people have browsed it

Reference: It means passing the address of the original object in memory to the target object, which is equivalent to the original object and the target object pointing to the same memory address. At this time, if the target object or the original object is modified, the data in the memory will also change.

The so-called value transfer means that only passing the value of the object to the target object is equivalent to copying; the system will re-open an identical memory space for the target object.

The so-called reference means passing the address of the object in memory to the target object, which is equivalent to making the target object and the original object correspond to the same memory storage space. At this time, if the target object is modified, the data in the memory will also change.

When passing a value, if it is a non-object, a copy of the value will be passed. Any changes to this variable will not affect the original value. Passing a reference or object means passing the real memory address. Changes to this variable will affect the original value.

function func1($a)
The code is as follows
 代码如下 复制代码

 

function func1($a)

{ $a = $a + 1; }

 function func2(&$a)

{ $a = $a + 1; }

$sample = 1;

func1($sample);

echo $sample; // 输出 1

$sample = 1;

func2($sample);

9.echo $sample; // 输出 2

Copy code

{ $a = $a + 1; }

function func2(&$a)

$sample = 1; func1($sample); echo $sample; // output 1 $sample = 1; func2($sample);
9.echo $sample; // Output 2
Summary In high school, the teacher told us that quoting is to give the memory address to the variable. As long as the memory address changes, the content of the variable we account for will also change. This is a bit like a house where things are placed, and we can all enter it. Take, as long as the things change, the things we see when we go in are the same, instead of me holding the things in my hands, so if the things in the room change, the things here will not change. http://www.bkjia.com/PHPjc/632649.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632649.htmlTechArticleReference: It means passing the address of the original object in memory to the target object, which is equivalent to the original object and the target The objects point to the same memory address. At this time, if the target object...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!