Home > Backend Development > PHP Tutorial > Detailed explanation of the meaning and function of adding the ampersand before the PHP function name?

Detailed explanation of the meaning and function of adding the ampersand before the PHP function name?

伊谢尔伦
Release: 2023-03-10 22:18:02
Original
4079 people have browsed it

In PHP, the & symbol passes a reference to a variable rather than a copy. Reference means accessing the same variable content with different names. This is not like C's pointers, which are symbol table aliases. Note that in PHP, the variable name and the variable content are different, so the same content can have different names. The closest analogy is Unix's filenames and the files themselves - the variable names are the directory entries, and the variable contents are the files themselves. References can be thought of as tight connections in a Unix file system.
PHP’s references allow you to use two variables to point to the same content. Meaning, when you do:

<?php 
$a =&$b 
?>
Copy after login

It means $a and $b point to the same variable.

Note: $a and $b are exactly the same here. This does not mean that $a points to $b or vice versa, but that $a and $b point to the same place.

The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later):

<?php 
$bar =& new fooclass(); 
$foo =& find_var ($bar); 
?>
Copy after login

Note: Not using the & operator results in A copy of the object is made. If you use $this in a class, it will apply to the current instance of that class. Assignment without & will copy the instance (e.g. object) and $this will be applied to the copy, which is not always the desired result. Due to performance and memory consumption issues, usually you only want to work on one instance.

Although you can use the @ operator to turn off any

error messages
in a constructor, such as with @new, this has no effect when using the &new statement. This is a limitation of the Zend engine and will cause a parsing error. The second thing a reference does is pass a variable by reference. This is accomplished by creating a local variable within the function and that variable references the same content in the calling scope. For example:

<?php 
function foo (&$var) { 
$var++; 
} 
$a=5; 
foo ($a); 
?>
Copy after login

will change $a to 6. This is because in function foo the variable $var points to the same thing that $a points to. See Passing by Reference for a more detailed explanation.

The third thing a reference does is reference return.



php function

What is the meaning of adding the ampersand in front of it? Below are two demonstration codes first, and then explanations.

Let’s explain the second function.

What $a=test(); gets in this way is not actually a reference return from the function, it is no different from an ordinary function call.


As for the reason: This is the regulation of PHP

php It is stipulated that what is obtained through $a=&test(); is the reference return of the function.


As for what is a reference return (

PHP Manual

says: Reference return is used when you want to use When the function finds which variable the reference should be bound to. )Using the above example to explain, calling the function in the

$a=test() method just assigns the value of the function to $a. That’s it, and any change in $a will not affect $b in the function.

And when calling the function through $a=&test(), its function is to return the memory of the $b variable in $b The address and the memory address of the $a variable,
point to the same place. That is to say, the effect is equivalent to this ($a=&b;). So changing the value of $a also changes the value of $b, so After executing:
$a=&test(); $a=5;, the value of $b becomes 5.

The above is the detailed content of Detailed explanation of the meaning and function of adding the ampersand before the PHP function name?. 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