Home >Backend Development >PHP Tutorial >What does it mean to add a number in front of a function when defining it? _PHP Tutorial

What does it mean to add a number in front of a function when defining it? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:51:041236browse




What does it mean to add an ampersand in front of a function when defining it?

If so, please give an example. Thank you


Best answer[url=http://www.111cn.cn/bbs/space.php?username=qxhy123]Link tag qxhy123[/url]
[url=http://www.111cn.cn/bbs/space.php?uid=66514]Link tag[img]http://www.111cn.cn/server/avatar.php?uid=66514&size=small[ /img][/url] Let me reveal the correct answer below. This is what I saw in the post of E Snail Children's Shoes on the forum. It accurately explains the function of adding an & in front of the function and the specific effect.

Function reference returns
Look at the code first and copy the PHP content to the clipboard
PHP code:
function &test()
{
static $b=0;//Declare a static variable
$b=$b+1;
echo $b;
return $b;
}

$a=test();//This statement will output that the value of $b is 1
$a=5;
$a=test();//This statement will output that the value of $b is 2

$a=&test();//This statement will output that the value of $b is 3
$a=5;
$a=test();//This statement will output that the value of $b is 6

Explain below:
In this way, $a=test(); actually does not get 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 stipulates 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 which variable the reference should be bound to.) This nonsense made me unable to understand it for a long time

To explain using the above example,
Calling a function using $a=test() only assigns the value of the function to $a, and any changes to $a will not affect $b
in the function. When calling a function through $a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place
That is to say, the effect equivalent to this is produced ($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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632607.htmlTechArticleWhen defining a function, add size=small[/img][/url] Let me reveal the correct answer below. This is what I saw in the post of E Snail Children's Shoes on the forum. It accurately explains that adding test() in front of the function { stati...
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