Understanding the Function Ampersand in PHP
In the code snippet provided, a Facebook library method begins with an ampersand (&) symbol. This ampersand has a specific meaning in PHP.
Meaning of an Ampersand in a Function Definition
An ampersand (&) preceding a function name signifies that the function will return a reference to a variable rather than the value itself.
Purpose of Return-by-Reference
Returning by reference is beneficial when you want a function to determine the variable to which a reference should be bound. However, it should not be used solely to enhance performance, as PHP automatically optimizes this. Return references only for valid technical reasons.
Using a Library with Return-by-Reference
In the example provided, the users_hasAppPermission method returns a reference to a variable that can be used within the calling function. For instance:
$permissions = &$facebook->users_hasAppPermission('publish_wall_post');
In this example, the variable $permissions will refer to the same variable as the one returned by the function.
The above is the detailed content of What Does the Ampersand (&) Mean in a PHP Function\'s Return Value?. For more information, please follow other related articles on the PHP Chinese website!