Home > Backend Development > PHP Tutorial > When does php pass by value or reference?

When does php pass by value or reference?

藏色散人
Release: 2023-04-06 18:20:01
Original
5426 people have browsed it

Variables are always assigned by value by default. That is, when the value of an expression is assigned to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, changing the value of one variable while the value of one variable is assigned to another variable will not affect the other variable.

When does php pass by value or reference?

#PHP also provides another way to assign values ​​to variables: reference assignment. This means that the new variable simply references (in other words, "aliases" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa. To use assignment by reference, simply add an & sign in front of the variable to be assigned (the source variable).

Objects are passed by reference by default. For larger data, it is better to pass by reference, which can save memory overhead.

Note:

Pass by value: Any changes to the value within the function will be ignored outside the function.

Pass by reference: Any changes to the value within the function will also reflect these modifications outside the function.

Application scenario: When passing by value, PHP must copy the value, but passing by reference does not need to copy the value, so passing by reference is generally used for large strings or objects.

Advantages and Disadvantages:

When passing by value, PHP must copy the value. This is expensive for large strings and objects. Passing by reference does not require copying the value, which is good for improving performance.

The above is the detailed content of When does php pass by value or reference?. For more information, please follow other related articles on the PHP Chinese website!

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