The magical use of PHP references
In: http://bardo.iteye.com/admin/blogs/848136? I have already described in detail what a reference is. Here is a talk about the magical use of references.
1, referring to the parameters, this has two functions, one, so that the external variables and functions in the variable synchronization changes. Second, the implementation has no return but can return multiple parameters.
2, for large data structure, reduce replication, thereby reducing memory consumption.
This is a common use. References in fact, there are some wonderful uses that are not known. We have just found one:
?
If there is code in a file, if we use it in the shell state:
$host =$_server[' Http_host '];
Notice information will appear in the log. Of course, you shut it down and it doesn't mean it doesn't produce. But in order for the web and shell to work, let's do this:
One
Use Error_reporting (245) to turn it off.
Second, directly prevent it from appearing:
$host =@$_server[' Http_host '];
Third, use the ternary operator.
$host = (isset ($_server[' http_host '))? $_server[' Http_host ']: ';
?
But this is a common approach.
In fact, we also have a way to let it not appear notice information:
?
$host =& $_server[' http_host '];
?
In this way, we guarantee that we will never use isset when we pass the argument, or @
?
We only use isset when the variables are actually used. This is not only a reduction in the number of code characters, but also a means of speeding up operational efficiency.
?
OK, now you might as well try it out, if the code below is running like this.
?
Error_reporting (E_all); $ar =array (); $b =& $ar [' Test ']; You can try the previous method in this line $c = ' all '. $b. ' "; Echo ($c);
?
Of course, using references, the biggest consideration is that you have to make sure that you can't change it in use. Otherwise, causes the original variable to synchronize the change, you must anticipate beforehand.
?
So, if it's not predictable, use @
?
1 Floor Input 2011-04-22
Really magical!