Abhi2011 Posted May 12, 2013 Share Posted May 12, 2013 Hello guys, I am learning PHP and what better way to do it than reading and understanding code. But I have a question what does the function do? -> Here is a sample code from MyBB $username = &$this->data['username']; And what does the & operator do? Thanks, Abhi2011 Quote Link to comment https://forums.phpfreaks.com/topic/277922-php-function/ Share on other sites More sharing options...
Solution Irate Posted May 12, 2013 Solution Share Posted May 12, 2013 Hello guys, I am learning PHP and what better way to do it than reading and understanding code. But I have a question what does the function do? -> Here is a sample code from MyBB $username = &$this->data['username']; And what does the & operator do? Thanks, Abhi2011 The -> operator is only available for objects and refers to modules, methods or variables of an object, $this is the currently used object, so $this->data['username'] selects the array key 'username' from the array data which is defined in the current object. The & operator can be used either as unary operator where it is used for bit shifting or as so-called reference passer, means &$this is the reference to $this, all modifications to $data will affect $this, too. Quote Link to comment https://forums.phpfreaks.com/topic/277922-php-function/#findComment-1429685 Share on other sites More sharing options...
Abhi2011 Posted May 12, 2013 Author Share Posted May 12, 2013 I don't think can be answered, (Not saying that you don't, saying that this might be foolish) but why not just use data['username'] I understood everything else. Quote Link to comment https://forums.phpfreaks.com/topic/277922-php-function/#findComment-1429688 Share on other sites More sharing options...
Irate Posted May 12, 2013 Share Posted May 12, 2013 Because you want to have data['username'] within the current object as there might exist other identically named arrays in other objects or in a global/local scope, imagine, username data can be used within many scripts. Quote Link to comment https://forums.phpfreaks.com/topic/277922-php-function/#findComment-1429689 Share on other sites More sharing options...
Abhi2011 Posted May 12, 2013 Author Share Posted May 12, 2013 Okay. Thanks for your help. Marking as solved. Quote Link to comment https://forums.phpfreaks.com/topic/277922-php-function/#findComment-1429692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.