There are two ways to pass values ​​in PHP, pass by value and pass by reference.

**熬夜选手
Release: 2020-05-01 14:40:42
Original
408 people have browsed it

The difference between value passing and reference passing in PHP

Variable passing by value means: "passing" the value of a variable to another variable.

There are only two ways to transfer data: Value transfer and Reference transfer. By default, PHP uses value passing.

Let’s take a look at the differences between these two delivery methods:

Value delivery:

That is to make a copy of the "data value" of the variable on the right, and then assign it to the variable on the left.

During the value pass (passl-by-value) process, the formal parameters of the called function are used as local to the called function Variable processing means opening up memory space on the stack to store the value of the actual parameter put in by the calling function, thus becoming a copy of the actual parameter. The characteristic of value transfer is that any operation of the called function on the formal parameters is performed as a local variable and will not affect the value of the actual parameter variable of the calling function.

Example:

$v1 = 1;
$v2 = $v1;
Copy after login

After the value is passed, the two variables have no influence on each other and are independent of each other

<?php
//值传递
$v1 = 10;
$v2 = $v1;
echo "v2的值为:".$v2;//10
$v1 = 11;
echo "<br>v2的值为:".$v2;//10
Copy after login

There are two ways to pass values ​​in PHP, pass by value and pass by reference.

Reference passing:

is to pass the reference relationship between the variable on the right to the data to the variable on the left

During the pass-by-reference process, Although the formal parameters of the called function also open up memory space on the stack as local variables, what is stored at this time is the address of the actual parameter variable put in by the calling function. Any operation of the called function on the formal parameters is processed as indirect addressing, that is, the actual parameter variables in the calling function are accessed through the address stored in the stack. Because of this, any operation the called function does on the formal parameters affects the calling function.

<?php
//引用传递
$v1 = 10;
$v2 = &$v1;
echo "v2的值为:".$v2;//10
$v1 = 11;
echo "<br>v2的值为:".$v2;//11
Copy after login

There are two ways to pass values ​​in PHP, pass by value and pass by reference.

Summary:

The main thing examined here is similar to passing by value. Quoted question. Understand that passing a value is to re-open the memory space, which is equivalent to copying the original value, and it is independent of the original value. Passing a reference (pointer) only adds a pointer to the original memory block. If the value of the reference changes, the memory address value pointed to is modified, and all pointed references have changed.

The above is the detailed content of There are two ways to pass values ​​in PHP, pass by value and pass by reference.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
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!