Unveiling the Meaning of Starting a PHP Function with an Ampersand
Encountering the ampersand (&) symbol at the beginning of a PHP function definition can raise questions. Let's explore the significance of this symbol and its implications.
What the Ampersand Represents
An ampersand before a function name indicates that the function returns a reference to a variable rather than the value itself. When a function returns by reference, modifications made to the returned variable will be reflected in the original variable.
Why Return by Reference?
Returning by reference is advantageous in scenarios where you desire the function to determine which variable should receive the reference assignment. However, it's crucial to avoid using return-by-reference solely for performance enhancement, as PHP will automatically optimize this process.
Using a Library with Return-by-Reference Functions
To utilize a library that employs return-by-reference functions, consider a simplified example involving the Facebook library:
$fb = new FacebookRestClient(); $ext_perm = 'some_permission'; $result = &$fb->users_hasAppPermission($ext_perm);
In this example, the function users_hasAppPermission returns a reference to a variable that holds the result of checking app permission for a particular user. By assigning the result to $result, any subsequent modifications to $result will also modify the original reference.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3