Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is '&' meaning in this php code

Tags:

php

wordpress

i using word press, and i try to learn it, but is see the code which i didn't understand

$post = &get_post($id);

i see '&' before the get_post, what is '&' meaning?

thank

like image 714
GusDeCooL Avatar asked Dec 04 '25 15:12

GusDeCooL


1 Answers

Here you're getting a reference to the result of get_post($id).

In particular you're using the "returning by reference" technique.

Returning References :

Returning by reference is useful when you want to use a function to find to which variable a reference should be bound.


The same syntax can be used with functions that return references, and with the new operator (since PHP 4.0.4 and before PHP 5.0.0):

  <?php
  $foo =& find_var($bar);
  ?>

Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_STRICT message.

So if you're using >= PHP 5 it's not a really great idea to use this notation.


Resources :

  • php.net - references
like image 135
Colin Hebert Avatar answered Dec 07 '25 04:12

Colin Hebert