Home > Article > Backend Development > Detailed explanation of the meaning and function of adding the ampersand before the PHP function name?
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 ?>
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); ?>
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); ?>
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.
As for the reason: This is the regulation of PHP
As for what is a reference return (
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!