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?

伊谢尔伦
伊谢尔伦Original
2017-06-21 10:53:134099browse

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 
?>

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); 
?>

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); 
?>

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.

function &chhua()
{
static $b="www.php.cn";//申明一个静态变量
$b=$b."WEB开发";
echo $b;
return $b;
}
  
$a=chhua();//这条语句会输出 $b的值 为“www.php.cnWEB开发”
$a="PHP";
echo "<Br>";
$a=chhua();//这条语 句会输出 $b的值 为“www.php.cnWEB开发WEB开发” 
echo "<Br>";
$a=&chhua();//这条语句会输出 $b的值 为“www.php.cnWEB开发WEB开发WEB开发”
echo "<Br>";
$a="JS";
$a=chhua(); //这条语句会输出 $b的值 为"JSWEB开发"
  
  
function &test()
{
    static $b=0;//申明一个静态变量
    $b=$b+1;
    echo $b;
    return $b;
}
  
$a=test();//这条语句会输出 $b的值 为1
$a=5;
$a=test();//这 条语句会输出 $b的值 为2
$a=&test();//这条语句会输出 $b的值 为3
$a=5;
$a=test(); //这条语句会输出 $b的值 为6
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!

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