What does this mean in PHP: -> or =>

PhpSyntax

Php Problem Overview


> Possible duplicate:
> where we use object operator “->” in php
> Reference - What does this symbol mean in PHP?

I see these in PHP all the time but I don't have a clue as to what they actually mean. What does -> do and what does => do. And I'm not talking about the operators. They're something else, but nobody seems to know...

Php Solutions


Solution 1 - Php

The double arrow operator, =>, is used as an access mechanism for arrays. This means that what is on the left side of it will have a corresponding value of what is on the right side of it in array context. This can be used to set values of any acceptable type into a corresponding index of an array. The index can be associative (string based) or numeric.

$myArray = array(
    0 => 'Big',
    1 => 'Small',
    2 => 'Up',
    3 => 'Down'
);

The object operator, ->, is used in object scope to access methods and properties of an object. It’s meaning is to say that what is on the right of the operator is a member of the object instantiated into the variable on the left side of the operator. Instantiated is the key term here.

// Create a new instance of MyObject into $obj
$obj = new MyObject();
// Set a property in the $obj object called thisProperty
$obj->thisProperty = 'Fred';
// Call a method of the $obj object named getProperty
$obj->getProperty();

Solution 2 - Php

-> is used to call a method, or access a property, on the object of a class

=> is used to assign values to the keys of an array

E.g.:

    $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34, 1=>2); 

And since PHP 7.4+ the operator => is used too for the added arrow functions, a more concise syntax for anonymous functions.

Solution 3 - Php

=> is used in associative array key value assignment. Take a look at:

http://php.net/manual/en/language.types.array.php.

-> is used to access an object method or property. Example: $obj->method().

Solution 4 - Php

->

calls/sets object variables. Ex:

$obj = new StdClass;
$obj->foo = 'bar';
var_dump($obj);

=> Sets key/value pairs for arrays. Ex:

$array = array(
	'foo' => 'bar'
);
var_dump($array);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilly KeatingeView Question on Stackoverflow
Solution 1 - Phpzafus_coderView Answer on Stackoverflow
Solution 2 - PhpSivagopal ManpragadaView Answer on Stackoverflow
Solution 3 - PhpDWrightView Answer on Stackoverflow
Solution 4 - PhpSamuel CookView Answer on Stackoverflow