Functional Decomposition of Functions Prepared by Signs in php

  • 2021-07-07 06:49:36
  • OfStack

php variable preceded by & Needless to say what the symbol means, everyone is using it, that is, two variables point to one address at the same time, then, php function is preceded by & What is the meaning of symbols? Here are two demo codes first, and then explain them.


function &chhua() 
{ 
static $b="www.ofstack.com";// Affirm 1 Static variables  
$b=$b."WEB Development "; 
echo $b; 
return $b; 
} 
 
$a=chhua();// This statement outputs $b The value of is " www.ofstack.comWEB Development "  
$a="PHP"; 
echo "<Br>";
$a=chhua();// This language   Sentences will be output $b The value of is " www.ofstack.comWEB Development WEB Development "   
echo "<Br>";
$a=&chhua();// This statement outputs $b The value of is " www.ofstack.comWEB Development WEB Development WEB Development "  
echo "<Br>";
$a="JS"; 
$a=chhua(); // This statement outputs $b The value of is "JSWEB Development "
 
 
function &test()
{
	static $b=0;// Affirm 1 Static variables 
	$b=$b+1;
	echo $b;
	return $b;
}
 
$a=test();// This statement outputs $b The value of is 1 
$a=5;
$a=test();// This   A statement outputs $b The value of is 2
$a=&test();// This statement outputs $b The value of is 3
$a=5;
$a=test(); // This statement outputs $b The value of is 6

Let's explain the second function under 1.
In this way $a=test (); What you get is not actually a reference return of a function, which is no different from ordinary function calls.

As for the reason: This is the regulation of PHP
php stipulates that through $a = & test (); Method is the reference return of the function.

As for what is reference return (PHP manual says that reference return is used when you want to use a function to find which variable the reference should be bound to.)

To explain it with the above example is
$a=test (), only assigning the value of the function to $a, and making any changes to $a will not affect $b in the function.
And through $a = & test (), whose function is to match the memory address of the $b variable in return $b with the memory address of the $a variable,
Points to the same place. That is, the equivalent of this effect is produced ($a= & b; ) So changing the value of $a also changes the value of $b, so after executing:
$a= & test (); $a=5; Later, the value of $b becomes 5.


Related articles: