In what situations can links be used in PHP? For example, what will be faster:

$get = &$_GET; echo $get['id']; // это? $get = $_GET; echo $get['id']; // или это? 

Please explain in which cases it is advantageous to use links, but I know about existence, but have never used it)

  • Incomprehensible question ... Links or GET requests? - Palmervan
  • This is just an example for $ _GET shown, maybe another array. - iproger
  • In general, I did not see the point in this ... echo $ _GET ['id']; - Shamanis
  • Solidarity) - Palmervan
  • Well, you give, what does it make sense, eh? I’m wondering why tags are used, I’ve seen it when I’ve put a link to global arrays, write off-topic here - iproger

4 answers 4

Well, here's a bunch of examples for you) The main use of links is to ease the code and save memory, because reference by reference does not copy the object.

 $arr = array(1, 2, 3); foreach ($arr as &$v) $v = 5; var_dump($arr); $array = array(array(2, 3, array(15, 16)), array(1, 2, 3)) function &array_get(&$arr) { $tmp = &$arr; for ($i = 1; $i < func_num_args(); $i++) { $code = func_get_arg($i); if (!isset($tmp[$code])) return false; $tmp = &$tmp[$code]; } return $tmp; } $var = &array_get($array, 0, 2, 0); echo $var; // 16 $var = 22; // изменит значение в массиве function my_recursive_procedure(&$array) { foreach ($array as &$v) if (is_array($v)) { my_recursive_procedure($v); } else $v = str_replace('a', 'b', $v); } // без ссылок было бы более громоздко + скопировался бы весь массив my_recursive_procedure($array); 
  • Thank you so much) - iproger
  • 2
    foreach ($ arr as & $ v) $ v = 5; When using a loop with a hard link, always unset at the end of the loop if you do not want to get a very subtle logical bug. Those. rightly so: foreach ($ arr as & $ v) {$ v = 5; unset ($ v); } - Ilya Pirogov

Before drawing conclusions

Yes, the links are very convenient, they also save memory

I strongly recommend that you carefully and carefully read this

@ Sh4dow - why you didn’t mention that such code is much worse read and, when using links, we lose about 30% of performance (in special cases even more).

Personally, I, in php, avoid links wherever possible and advise you ...

  • You will find among my options the use of 2 links to 1 object?) Habr Habr, and even there perverts there is, and the code described in the article is illogical. If you need 2 objects - you need to do 2 objects. By the way, $ a = array (1, 2, 3); $ b = & $ a; // note that the article is different foreach ($ a as & $ val) $ val = 5; var_dump ($ b); // 5, 5, 5 =) there was no copying. And the curved hands - this is to the dohtor already. It is not the links that should be avoided, but the stupid code :) The bottom line: do not use links where they are not needed. Just like any other element of the language. - Sh4dow
  • Thank you, I will consider) - iproger
  • The bottom line is that you can write any code without using them, so they are not needed (in any case, I almost never use them) - Zowie
  • one
    I highly recommend reading the article by Derik Retansa (PDF) , the developer of the xdebug extension, which details the mechanism of how PHP variables work and why variables using a hard link can greatly increase memory consumption. > foreach ($ arr as & $ v)> $ v = 5; When using a loop with a hard link, always unset at the end of the loop if you do not want to get a very subtle logical bug. foreach ($ arr as & $ v) {$ v = 5; unset ($ v); } - Ilya Pirogov

References Explanations

I also advise everyone who has not read yet to read the article by Derik Retansa References in PHP

    Links are needed to pass variables in a function and inside the function to work with the variable as global, but without a global $ var declaration;

    • well, thank you, except perhaps for this?)) - iproger