What is '&' meaning in this php code
i using word press, and i try to learn it, but is see the code which i didn't unde开发者_Python百科rstand
$post = &get_post($id);
i see '&' before the get_post, what is '&' meaning?
thank
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 anE_STRICT
message.
So if you're using >= PHP 5
it's not a really great idea to use this notation.
Resources :
- php.net - references
Passing or assigning by reference.
Now you already know that is for return by reference, next things is the effect of that. The effect will be this: Whenever you change $post variable's value, you are actually changing the get_post($id) content. get_post() return object having all the info about that post in database.
精彩评论