2

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I've been programming with PHP for a bit now, but every once in a while I run across these two expressions:

->
=>

What are these and what do they mean/do? I don't even know what to call them in order to find out...

Community
  • 1
  • 1
dcolumbus
  • 9,596
  • 26
  • 100
  • 165
  • 1
    http://stackoverflow.com/questions/2588149/in-php-what-is-the-operator-called-and-how-do-you-say-it-when-reading-code – eisberg Apr 01 '11 at 06:49
  • 2
    As for `->`, see [what does this mean in php: ->](http://stackoverflow.com/questions/3910147/what-does-this-mean-in-php). – jensgram Apr 01 '11 at 06:49

4 Answers4

5

-> is used to access instance attributes of objects. It is the equivalent to the . syntax in many other languages (C, C++, Python, Javascript).

$myclass->my_instance_var;
$myclass->my_instance_method();

=> is used to map keys to values in associative arrays. It is the equivalent of : in a mapping in Python and Javascript.

$arr = Array("Hello" => "World", "Foo" => "Bar");
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • Do you have an example of both? I'm not seeing why I would need to use them. – dcolumbus Apr 01 '11 at 06:51
  • _associative arrays_ this is not a restriction you can use with numerical index array as well – Shakti Singh Apr 01 '11 at 06:53
  • @Shakti when you specify numerical indices, those are associative arrays. Technically, in PHP, all arrays are associative, but there's a special case where keys are assigned for you. @dcolumbus you'd use the first if you write or use classes; you'd use the seconds if you use associative arrays. – Rafe Kettler Apr 01 '11 at 06:54
5

-> this symbol is used to refer the property or method of object

$obj->age=25;
$obj->setAge();

=> this symbol is used to assign values in array

$array=array('age'=>25,'name'=>'test');
$array=array(10=>20, 30=>50);
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • So in PHP, do I have to use the "->" in order to call a method within a class? In ActionScript, all I have to do is: obj.function(); – dcolumbus Apr 01 '11 at 06:53
  • 2
    @dcolumbus: Yes, But the static method are exception in this case they can be called with `classname::statcimethod()` – Shakti Singh Apr 01 '11 at 06:54
1

They are both operators.

The => is an assigment operator for arrays, assigning values to a named key. Have a look at http://www.php.net/manual/en/language.operators.assignment.php

The -> is an accessor so if you have a class Foo with a variable Bar you would access that using the -> operator:

// Get value of Bar $value = $fooInstance->Bar

Basically the -> operator is similar to the "." in Java and C#

Esben Bach
  • 664
  • 4
  • 23
1

-> is for accessing attributes and methods of an object:

class myClass
{
    public $anAttribute = 'hey this is my attribute';

    public function myMethod()
    {
        return 'this is my method';
    }

}

$class = new myClass();
echo $class->anAttribute;
echo $class->myMethod();

=> is used in two places. This can be while instantiating an array manually or dynamically in an foreach-statement:

// Manually instantiated:
$myArray = new array('fruit' => 'apple', 'meat' => 'sausage');
echo $myArray['fruit'];
echo $myArray['meat'];

// Dynamic in foreach
foreach($myArray AS $key => $value)
{
    echo "myArray['$key'] is $value";
}
Fidi
  • 5,754
  • 1
  • 18
  • 25