search
HomeDaily ProgrammingPHP KnowledgeThere are two ways to pass values ​​in PHP, pass by value and pass by reference.

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;

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

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

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools