How to use -> and => in PHP
February 24th, 2006 by Sarah KingI just saw a forum post asking the difference between -> and => and it's one of those tricky questions to research, just how can a search engine understand what you're asking?
Well, luckily the answer is quite simple.
-> is used by objects to set, get or call a method of that object.
Ref: Chapter 18. Classes and Objects (PHP 4)
=> is used by arrays to describe the relationship between the key and the value
Ref: Arrays
Here's an example of how the -> is used in a class
PHP:
-
class fruit{
-
var $color;
-
var $price;
-
var $quantity;
-
-
function getTotal()
-
{
-
$total = $this->price * $this->quantity;
-
return $total;
-
}
-
}
-
-
$apple = new Fruit();
-
$apple->color = 'red';
-
$apple->price = 1;
and here is how => is used in an array. Despite constructing $apple and $pear in different ways, the arrays are structurally the same and should output the same information when the script is run.
PHP:
