About the two symbols of php @ and $---series that php will always know

When writing code, I encountered the problem of @ and $ in front of functions and variables, so I took this opportunity to learn the two methods of php passing by value and passing by reference.

 

 

First of all

 

The @ operator is only valid for expressions. A simple rule for novices is: if you can get a value from somewhere, you can add the @ operator in front of it. 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 invalidates even error reporting of serious errors that caused the script to terminate. This means that if you use "@" to suppress the error message before a function call that does not exist or is of the wrong type, the script will die there without any indication of the cause.

In a word, it is a means to suppress error reporting

 

 

 

For $ is our key issue

 

PHP code:
Basic form:
function &find_var($param) //Here, & is used in front of the object, which acts as a declaration
{     /* ...code... */     return $found_var; } $foo =& find_var($bar) ; //Here, & is used to link variables $foo->x = 2; //Assign a value to a function Example: function &test()    {    static $b=0;//Declare a static variable    $b=$b+1 ;    echo $b;    return $b;    }    $a=test();//This statement will output the value of $b as 1, $a=5;    $a=test();//This statement will output The value of $b is 2, because there is no return by reference, $a=5, which does not assign value to the object $a=&test();//This statement will output the value of $b as 3, and the successful reference returns, the following code It works $a=5;    $a=test();//This statement will output the value of $b as 6   as explained below:  In this way, $a=test(); is actually not a reference return of the function , This is no different from ordinary function calls. As for the reason: this is the rule of PHP,





 
















PHP stipulates that the return by reference of the function is obtained through the method of $a=&test();.
As for what is 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 bullshit made me fail to understand for a long time.
Using the above example to explain that the
function is called by $a=test(), just assigning the value of the function to $a, and any change in $a will not affect the $b in the function. Pass $a=&test () method to call the function, his function is to transfer the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, producing an effect 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 ......

php What does the ampersand in front of the function mean? Some functions will be added with ampersands, because I rarely see them, so I don’t understand the meaning of
adding ampersands in front of php functions. So, what is the effect of adding ampersands in front of php functions?  

Java code 
function &test()   
{   
static $b=0;//Declare a static variable   
$b=$b+1;   
echo $b;   
return $b;   
}   
  
$a=&test();//This statement will output The value of $b is 1   
$a=6;   
test();//This statement will output the value of $b as 7 
 
plus & before the php variable


Let’s look at an example


Php code 

 
$foo = 321;
$bar = &$foo; 
$bar = 123;
print $foo; Then what will be the output result? Why is


Php code 
123    
  
like this?


Changing the new variable will affect the original variable, and this assignment operation is faster.
Note: Only named variables can be assigned by address, that is, changing the value of $bar will also change the value of $foo.
 
Another example:

$_GET[1] = 1;
function &a()
{  $a = $_GET[1];  return $a; } $x =& a(); $x ='MoontoC'; echo $_GET[ 1]; // MoontoC will be displayed here instead of the initially assigned value 1. Do you understand the meaning? When using a function to pass a value, both parties must use the reference symbol to make sense, so that it can be truly quoted, and any side is less If you use the reference symbol, you will not get the wrong content, but the content is passed by value, not by reference. It is really difficult for people who have no program foundation to understand the importance of passing by value and passing by reference at first. They feel that they can get what they want anyway, but it is not. Many times, although they get the same thing, the price is completely different. A value of up to 2 million words is transferred as a value, which means that 4 million words are placed in the memory at the same time, which means that the memory is doubled, and passing by reference is just a shortcut to transfer it.





 

 

Explanation 2:

 

 

Just add the ampersand in front of variables, functions, objects, etc.


Quoting in PHP means: different names access the same variable content. It 
is different from pointers in C language. The pointer in the C language stores the content of the variable. The address stored in the memory. 

The reference of the variable. 

PHP reference allows you to use two variables to point to the same content. 
[php]  
<? 
$a="ABC"; 
$b = &$a; 
echo $a;//output here: ABC 
echo $b;//output here: ABC 
$b="EFG"; 
echo $a;//here the value of $a becomes EFG so output EFG 
echo $ b;//The output of EFG here 
?> 
[/php] The 

function's call 
by address. I won't say much about it. The code is directly given below 
[php] 
function test(&$a) 

$a=$a+100 ; 

$b=1; 
echo $b;//output 1 
test($b); //here $b is passed to the function is actually the memory address where the variable content of $b is located, by changing $a in the function Can change the value of $b 
echo "<br>"; 
echo $b;//output 101 
[/php] 
It should be noted that there will be an error in test(1);, the reason is to think about the 

function reference return 
first look at the code 
[php] 
function &test() 

static $b=0;//Declare a static variable 
$b =$b+1; 
echo $b; 
return $b; 


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

$a=&test();//This statement will output the value of $b as 3 
$a=5; 
$a=test();//This statement will output $ The value of b is 6 
[/php] 
The following explains:  
In this way $a=test(); What you get is actually not a reference return of a function, which is no different from ordinary function calls. As for the reason: This is the stipulation of 
PHP It is stipulated that what is obtained by the method of $a=&test(); is the reference return of the function. 
As for what is the 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. .) I didn’t understand this bullshit for a long time. 

Using the above example to explain it is 
to call the function with $a=test(), just assign the value of the function to $a, and any changes to $a will not affect To $b in the function 
And by calling the function through $a=&test(), his role is to point the memory address of the $b variable in return $b to the same place
as the memory address of the $a variable,  which produces an effect 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 has changed to 5 

here for everyone Use static variables only when you understand the reference return of a function. In fact, the reference return of a function is mostly used in the object 

reference 
[php] 
<? 
class a{ 
var $abc="ABC"; 

$b=new a; 
$c=$ b; 
echo $b->abc;//here output ABC 
echo $c->abc;//here output ABC 
$b->abc="DEF"; 
echo $c->abc;//here output DEF 
?> 
[/php] The 
above code is running 
in PHP5. The copying of objects in PHP5 is realized by reference. In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b; The 
default in PHP5 is to call objects by reference, but sometimes you may want to create an object It is hoped that changes to the original object will not affect the copy. For this purpose, PHP defines a special method called __clone. 


If the program is relatively large, there are many variables that refer to the same object, and you want to manually clear it after using the object, I personally recommend using the "&" method, and then use the $var=null method to clear it. Other times, use the php5 default Way. In addition, for the transmission of large arrays in php5, it is recommended to use the "&" method, which saves memory space usage after all. 


Dereference 
When you unset the reference, just break the binding between variable name and variable content. This does not mean that the variable content is destroyed. For example:  

<?php 
$a = 1; 
$b =& $a; 
unset ($a); 
?>   

will not unset $b, just $a.  


Global references
When a variable is declared with global $var, a reference  to the global variable is actually established. In other words, it is the same as doing this:  

<?php 
$var =& $GLOBALS["var"]; 
?>   

This means that, for example, unset $var will not unset global variables.  

$ this 
In an object method in, $ this is always a call to its object. 


//Following another episode 
. The function of pointing to the address (similar to a pointer) in PHP is not implemented by the user, but implemented by the Zend core. The reference in PHP uses the principle of "copy on write", that is, unless it happens For write operations, variables or objects pointing to the same address will not be copied. 

Popular talk 
1: 

$a="ABC"; 
$b=$a; 
[/php] 
In fact, both $a and $b point to the same memory address at this time instead of $a and $b occupying different memory 

2: If in the above code On the basis, add the following code 
[php]  
$a="EFG"; 
[/php] 
Since the data in the memory pointed to by $a and $b has to be written again, the Zend core will automatically determine that it is automatically $b Produce a data copy of $a and reapply for a piece of memory for storage 

Guess you like

Origin blog.csdn.net/ld17822307870/article/details/113012402