In what situations can I use links to PHP? For example, what will be faster:
$ get = & amp; $ _ get;
Echo $ GET ['ID']; // this is?
$ get = $ _Get;
Echo $ GET ['ID']; // or that?
Please explain in what cases it is profitable to use links, but I know about the existence, but never used)
Answer 1, Authority 100%
Well, here is a bunch of examples) the basic applications of links are to facilitate code and memory savings, because The reference to the link does not copy the object.
$ arr = array (1, 2, 3);
Foreach ($ ARR AS & AMP; $ V)
$ V = 5;
VAR_DUMP ($ ARR);
$ Array = Array (Array (2, 3, Array (15, 16)), Array (1, 2, 3))
FUNCTION & AMP; Array_get (& amp; $ arr) {
$ TMP = & amp; $ arr;
For ($ i = 1; $ i & lt; func_num_args (); $ i ++) {
$ Code = FUNC_GET_ARG ($ i);
If (! isset ($ TMP [$ Code])) RETURN FALSE;
$ TMP = & amp; $ TMP [$ Code];
}
Return $ TMP;
}
$ var = & amp; array_get ($ array, 0, 2, 0);
Echo $ var; // 16
$ var = 22; // Change the value in the array
Function My_Recursive_ProCedure (& amp; $ array) {
Foreach ($ array as & amp; $ V)
if (IS_Array ($ V)) {
My_Recursive_ProCedure ($ V);
} ELSE $ V = STR_REPLACE ('A', 'B', $ V);
} // Without references, it would be more cumbersome + I would have copied the entire array
My_Recursive_ProCedure ($ array);
Answer 2, Authority 133%
Before making the conclusions
Yes Links are very comfortable, also memory saves
I strongly recommend, thoughtfully and carefully read here it
@ sh4dow – Why didn’t you mention that such code is much It is worse and, when using references, we lose about 30% of performance (in special cases even more).
Personally, I, in PHP, I avoid links wherever possible and advise you …
Answer 3, Authority 100%
I also advise everyone who has not yet read, read the article Derika Rattance references in php
Answer 4, Authority 33%
References are needed to transmit variables in the function and inside the function to work with a variable as global, but without an ad Global $ var;