I 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;
echo $apple->getTotal();[/php]
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]$apple = array();
$apple[‘price’] = 1;
$apple[‘color’] = ‘red’;
$pear = array(‘price’ => 2, ‘color’ => ‘green’);
foreach($apple as $key => $val) {
echo “{$key}: {$val}
“;
}
foreach($pear as $key => $val) {
echo “{$key}: {$val}
“;
}
[/php]
Be First to Comment