What is the use of the ampersand before the php function?

巴扎黑
Release: 2023-03-14 17:02:02
Original
1543 people have browsed it

This article mainly introduces the decomposition of the ampersand symbol before the function in php. Its function is called reference return, which is a bit abstract. Please see the content of this article for a detailed explanation. Friends in need can refer to

php Needless to say, what does adding an ampersand in front of a variable mean? Everyone is using it. It means that two variables point to the same address at the same time. So, what is the meaning of adding an ampersand in front of a php function? Below are two demonstration codes first, and then explanations.


function &chhua() 
{ 
static $b="www.jb51.net";//申明一个静态变量 
$b=$b."WEB开发"; 
echo $b; 
return $b; 
} 
 
$a=chhua();//这条语句会输出 $b的值 为“www.jb51.netWEB开发” 
$a="PHP"; 
echo "<Br>";
$a=chhua();//这条语 句会输出 $b的值 为“www.jb51.netWEB开发WEB开发”  
echo "<Br>";
$a=&chhua();//这条语句会输出 $b的值 为“www.jb51.netWEB开发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
Copy after login

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 (the PHP manual says: Reference return is used when you want to use a function to find a reference and it should be bound When determining which variable it is on.)

Using the above example to explain, calling the function in the
$a=test() method just assigns the value of the function to $a, and $a does Any change will not affect $b.
in the function. When calling the function through $a=&test(), its function is to compare the memory address of the $b variable in return $b with the $a variable. The memory address,
points 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; From now on, the value of $b becomes 5.

The above is the detailed content of What is the use of the ampersand before the php function?. 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