Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between passing values and passing addresses in php

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what is the difference between value and address in php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the difference between value and address in php?"

Difference: pass the value is to copy the contents of the original variable, and then use a new memory space to save, the two variables are independent of each other, modify one of the variables will not affect the other. Passing an address (reference passing) is equivalent to giving an alias to the current variable. In fact, these two variables refer to a value, and modifying one variable will affect the other.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Value transfer

Value passing is the default way for functions to pass values in PHP, also known as "copy passing values". As the name implies, the way the value is passed will copy the value of the argument and then pass it to the parameter of the function, so the value of the operation parameter in the function will not affect the argument outside the function. So if you don't want the function to change the value of the argument, you can pass it by value.

[example] A simple function is defined below. The function has two parameters and exchanges the values of the parameters in the function.

The running results are as follows:

Outside the function, before the exchange $x = 5, $y = 7 function, before the exchange $a = 5, $b = 7 function, after the exchange $a = 7, $b = 5 function, after the exchange $x = 5, $y = 7

From the running results, we can see that within the function, the values are indeed exchanged, while outside the function, the values do not change. Therefore, it can be said that the value of the function is passed only a copy of the variable. So if you want the function to be able to manipulate arguments outside the function, you need to use reference passing.

Address passing (reference passing)

The reference passing of the parameter is to copy a copy of the memory address of the argument, and then pass it to the formal parameter of the function. Both the parameter and the parameter point to the same memory address, so the operation of the function on the parameter will affect the argument outside the function.

Passing by reference is passing the memory address of the argument to the formal parameter of the function. So the argument and the parameter point to the same memory address. At this point, all operations inside the function will affect the value of the arguments outside the function. References are passed by adding an & symbol to the value, as shown below:

Function name (& Parameter 1, & Parameter 2,..., & Parameter 3) {.}

[example] slightly adjust the code of the above example to pass parameters to the swap function by reference passing, as follows:

The running results are as follows:

Outside the function, before the exchange $x = 5, $y = 7 function, before the exchange $a = 5, $b = 7 function, after the exchange $a = 7, $b = 5 function, after the exchange $x = 7, $y = 5 to this point, I believe you have a deeper understanding of the "php value and address of the difference", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report