Hi all!
I'm totally new to PHP and I'm starting to learn it. I've been programing with ASP an other languanges for some time, but there're some things in PHP that are new to me and I'm a little confused.
I've seen some code examples were I can see this "->" or "=>". I've not seen this in any other programming languages and I'm not sure how it works. I've found it mainly when working with arrays.
Can somebody explain me how this works? Thanks!
Question about PHP syntax
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Question about PHP syntax
You'll see => when working with arrays, -> when working with objects.Dreamboy wrote:I've seen some code examples were I can see this "->" or "=>". I've not seen this in any other programming languages and I'm not sure how it works. I've found it mainly when working with arrays.
=> is an array assignment operator (for want of a better description) that allows you to specify the value associated with a particular key.
Code: Select all
$myArray = array(1 => 'one', 2 => 'two')-> is used to access the properties or methods of an object
Code: Select all
class myClass {
public $classAttribute = 'value';
public classMethod($value=null) {
$this->classAttribute = $value
}
}
$myObject = new myClass();
$myObject->classMethod('result');
echo $myObject->classAttribute;
Re: Question about PHP syntax
Hi Mark!
Thanks for your excelent and clear explanation.
Now I know that => is used to assign values to the array (a very nice and logical way to do this) and -> is used to acces properties of methods. This is the same as the "dot" syntax in other programming languanges. I've found it also logical, but maybe is a little harder to write the -> than the "."
One more lesson to go!
Thanks for your excelent and clear explanation.
Now I know that => is used to assign values to the array (a very nice and logical way to do this) and -> is used to acces properties of methods. This is the same as the "dot" syntax in other programming languanges. I've found it also logical, but maybe is a little harder to write the -> than the "."
One more lesson to go!