Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between adding @ and $ symbols in front of functions and variables in PHP
@ Operator is only valid for expression. A simple rule for beginners is: if you can get a value from somewhere, prepend it with the @ operator. For example, you can put it before variables, functions and include() calls, constants, etc. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach.
The current "@" error control operator prefix even disables error reporting of serious errors that cause the script to terminate. This means that if "@" is used to suppress error messages before a function call that does not exist or is of the wrong type, the script will die there without any indication of the reason.
For $, it is our key question
Basic form:
function &find_var($param) //这里用&在对象前面,作用为声明 { /* ...code... */ return $found_var; } $foo =& find_var($bar); //这里用&,作用为联系变量 $foo->x = 2; //赋值给函数
Example:
function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条语句会输出$b的值为1, $a=5; $a=test();//这条语句会输出$b的值为2 ,因为没有引用返回,$a=5,不对对象起赋值作用 $a=&test();//这条语句会输出$b的值为3 ,成功引用返回,下面的代码起作用 $a=5; $a=test();//这条语句会输出$b的值为6
Explained below Next:
What you get in this way $a=test(); is not actually a function reference return, which is no different from an ordinary function call. As for the reason: This is a 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.)
Use the above example to explain:
$a=test() method calls a function, which just assigns the value of the function to $a. Any changes to $a will not affect $b in the function. How about calling a function through $a=&test() method? , His function is to move the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, producing the equivalent of this effect ($a=&b;) So change the $a The value also changes the value of $b at the same time, so after executing: $a=&test();
$a=5;
, the value of $b becomes 5...
So, what is the effect of adding the ampersand in front of the php function?
What does it mean to add an ampersand in front of a php variable
Let’s look at an example first:
$foo = 321; $bar = &$foo; $bar = 123; print $foo; //那么输出的结果将会是什么呢
Changing new variables will affect the original variables. This assignment operation is faster.
Note: Only named variables can be assigned by address. That is to say, changing the value of $bar also changes the value of $foo.
Another example:
$_GET[1] = 1; function &a() { $a = $_GET[1]; return $a; } $x =& a(); $x = 'MoontoC'; echo $_GET[1]; // 此时这里会显示 MoontoC, 而不是最初赋值的1
Do you understand the meaning? When using functionsWhen passing values, both parties must use reference symbols to make sense. A real reference, and if there is no reference symbol on either side, you will not get the wrong content, but the content is passed by value, not by reference. It is indeed difficult for people who have no programming foundation to understand the importance of passing by value and passing by reference when they first learn PHP. They feel that they can get what they want anyway, but this is not the case. In many cases, although they get the same thing, the price is completely different. If a value of 2 million words is transmitted as a value, 4 million words are stored in the memory at the same time for use, which means that it consumes twice as much memory, and passing a reference is just a shortcut to transfer it.
The above is the detailed content of Detailed explanation of the difference between adding @ and $ symbols in front of functions and variables in PHP. For more information, please follow other related articles on the PHP Chinese website!